aboutsummaryrefslogtreecommitdiff
path: root/content/post/nixos-p1/index.md
blob: 920ca94537badfb2ee7e425d7bbf819e21aef4ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
+++
author = "Kumar Damani"
title = "Adventures in NixOS - Part 1"
date = "2023-03-24"
draft = false
description = "Guide to getting started with NixOS the modern way."
tags = [
    "technology"
]
+++

Almost a guide to getting started with NixOS the modern (2023) way.
<!--more-->

There seems to be a shortage of written guides on the Internet for setting up NixOS
the "modern" way - Flakes + Home Manager. It doesn't help that Nix's official
docs are very disjointed so hopefully this will fill in some gaps that I observed 
when setting it all up.

![meme](fox.jpg)

> Warning: NixOS is not exactly beginner friendly - 
you should have familiarity installing Linux distros before trying this.

## But Why NixOS?
Eh... FOMO regarding all the [memes](https://www.google.com/search?q=nixos+memes&tbm=isch).

There is one particular feature that is intriguing -
the ability to roll-back your entire system (not incl. BIOS) in case of a 
misconfiguration or a broken update. **You can't do this with Ansible.**
This allows you (in theory) to get the benefits of a rolling release as well as the
stability benefits of a traditional distro.

> It takes the idea of reproducible builds, and extends it to the OS.

For example, NixOS will present you with all previous "builds" at boot time 
for you to revert to in case something gets messed up.
![boot prompt](boot.png)

## Things I want to explore as part of this exercise:
1. How hard is it to go from a minimal install to productive?
At minimum I need a graphical environment with working vol, mic, camera, wifi, 
and hibernation. 
Is it harder than doing the same in something like Manjaro?
2. Is Wayland truly ready?
3. Try out [`dwl`](https://github.com/djpohly/dwl) (the Wayland port of `dwm` by the [suckless](https://suckless.org) folks) 

## Constraints
1. I want to stick to the "Nix" way of doing things as much as possible
*where it makes sense to me*.
2. Stick to Wayland only applications as much as possible.

## Spoilers! (End result)
![my rice](rice.png)
(Probably too ugly for r/unixporn, but works for me :)

## Installation
### Getting a Live USB going
* I used the [minimal iso](https://nixos.org/download.html#nixos-iso).
* Create a bootable USB with the usual - `sudo dd if=/path/to/iso of=/dev/sdX bs=4M`.
* Boot up. 
* Then start following the steps for [manual installation](https://nixos.org/manual/nixos/stable/index.html#sec-installation-manual)
from the official guide to complete the install.

> Make sure to give SWAP as much space as your memory capacity for hibernation to work properly.

> Wifi did not work for me out of the box. So I used my phone to tether via USB. 

## First boot
If everything went well, you should be presented with a TTY prompting you to login:
```
NixOS ...
Login: <your username>
Password: <your password>
```
Once you login, you will still just have a TTY, but we can now go ahead and start
installing our graphical environment. 

## Housekeeping
* You should have two NixOS config files in `/etc/nixos/`:
```
configuration.nix
hardware-configuration.nix
```

* Edit the `configuration.nix` file by setting the correct values for hostname, networking, timezone and users. I also add a few basic system-wide packages here such as `git, rsync, neovim, htop` etc.

* To *apply and use* your changes to any of these files you need to run:
```bash
sudo nixos-rebuild switch
```
* Since we want all our configuration to be version controlled,
I copied these files to live under my user's config: 
`~/.config/{nix, nixpkgs}/`:
```bash
cp /etc/nix/nix.conf ~/.config/nix/nix.conf
cp /etc/nixos/configuration.nix ~/.config/nixpkgs/configuration.nix
```

Nix will now use these user-specific files to read its config :)

### Flakes Support
Nix (the pkg mgr) does not come with Flakes support out-of-the-box. 
So we need to enable it: 
1. In `~/.config/nix/nix.conf` add:
```
experimental-features = nix-command flakes
```

2. Apply it:
```bash
sudo nixos-rebuild switch
```
Read about [Flakes](https://nixos.wiki/wiki/Flakes).

### Home Manager Support
Now we can install the Home-Manager flake.
1. Init our base flake:
```bash
cd ~/.config/nixpkgs
nix flake init
```
This should generate two files:
```
flake.nix
flake.lock
```
2. Next we tell `flake.nix` to manage all our configuration (system + home) for our system:
```nix
{
  description = "NixOS configuration";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    home-manager.url = "github:nix-community/home-manager";
    home-manager.inputs.nixpkgs.follows = "nixpkgs";
    nixos-hardware.url = "github:NixOS/nixos-hardware/master";
  };

  outputs = inputs@{ nixpkgs, home-manager, nixos-hardware, ... }: {
    nixosConfigurations = {
      # Change below to use your hostname from configuration.nix
      "art-sr" = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          ./configuration.nix
          nixos-hardware.nixosModules.framework
          home-manager.nixosModules.home-manager
          {
            home-manager.useGlobalPkgs = true;
            home-manager.useUserPackages = true;
      	    # Change below to use your username from configuration.nix
            home-manager.users.kdam0 = import ./home.nix;

            # Optionally, use home-manager.extraSpecialArgs to pass
            # arguments to home.nix
          }
        ];
      };
    };
  };
}
```
>"art-sr" is my hostname for this machine.

In my case, I also have the following two lines specifically to load settings for my hardware:
```
    nixos-hardware.url = "github:NixOS/nixos-hardware/master";
    ...
          nixos-hardware.nixosModules.framework
```
you will need to modify these values based on your [hardware support](https://github.com/NixOS/nixos-hardware).

3. Create a `~/.config/nixpkgs/home.nix` file with your values:
```nix
{ config, pkgs, ...}: 
{
  home.username = "kdam0";
  home.homeDirectory = "/home/kdam0";

  programs.home-manager.enable = true;
  home.stateVersion = "22.11";

  services.gpg-agent = {
    enable = true;
    defaultCacheTtl = 1800;
    enableSshSupport = true;
  };
}
```

4. Apply:
```bash
sudo nixos-rebuild switch
```
> It took a few attempts to get NixOS to re-build successfully. 
I ran into a few different issues involving users, and hostnames 
while following the official docs until I arrived at the configs above which worked.

Read about [Home Manager flake](https://nix-community.github.io/home-manager/index.html#ch-nix-flakes).

**This would be a good time init a git repo in `~/.config/nixpkgs/` and publish your progress.**

## Setting up the GUI
### Window Manager + bar + system info.
Getting `dwl` is easy enough. In my `home.nix`:
```
  home.packages = [
    pkgs.dwl
    ...
  ];
```
This will install `dwl` on a rebuild. 
Then I can run it with `dwl`. 

Oh it fails...something about permissions...
In `~/.config/nixpkgs/configuration.nix` make sure you have:
```
  security.polkit.enable = true;
```

Oh it fails again with GLE errors :( Add:
```
  hardware.opengl = {
    enable = true;
    driSupport = true;
  };
```

**Sweet now it launches!**

But of course I need to configure the keys - that's the whole point of a WM.
Additionally, `dwl` requires a re-build on every config change...

**How do I tell Nix to use my config file while installing/building `dwl`?**

Lucking all packages (`pkgs.*`) build files are defined on their GitHub.
We can see that we are allowed to pass in a `conf` argument to
[this](https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/window-managers/dwl/default.nix)
file.

After a bit of digging around, I arrived at how to do it:

1. First copy your `config.h` to `~/.config/nixpkgs/dwl/config.h`.
2. Then point to it in your `home.nix`:
```
  home.packages = [
    (pkgs.dwl.override {
      # trying to supply config.home.homeDirectory here leads to "impure" usage.
      # so disabling it for now.
      # conf = (builtins.readFile "${config.home.homeDirectory}/.config/dwl/config.h");
      conf = ./dwl/config.h;
    })
    ...
  ];
```

> I know I can just clone the source myself and build it, but I like to use the default
package manager whenever possible to manage packages.

Of course since `dwl` is as minimal as it gets, it does not ship with a bar.
We have many options for which bar to use, 
I have very little use for a bar, so I kept it very simple and went with `somebar`:
```
  home.packages = [
    ...
    pkgs.somebar
    ...
  ];
```

If you want status info on your bar you can use something like `someblocks` - which will 
let you script simple scripts with text output you want displayed in each block.
This will need to be cloned and built manually as it is not available in the Nix repos.

Oh but you probably don't have `make` or any requirements to actually build it...fear not:
```bash
nix-shell -p gnumake
```
which put you in a temporary environment with all the common build tools available.
Now you can:
```bash
sudo make install
```

`wbg` is a simple background setter for Wayland......aaaand BAM!

![dwl pic](dwl.png)

### Terminal
I use [`foot`](https://codeberg.org/dnkl/foot).

Create the config file in `~/.config/nixpkgs/foot/foot.ini`:
```
# for transparency #
[colors]
alpha=0.7
```

Use it in `home.nix`:
```
  home.file.".config/foot/foot.ini".source = ../../common/foot/foot.ini;
```

> I use this pattern for pretty much all my *dotfiles*:
```
  # script that sets a bg.
  home.file."bg.sh".source = common/bg.sh;
  # script that starts my gui env.
  home.file."start.sh".source = common/start.sh;
  # foot config
  home.file.".config/foot/foot.ini".source = common/foot/foot.ini;
  # wofi config (app launcher)
  home.file.".config/wofi/style.css".source = common/wofi/style.css;
  # mako config (notifications)
  home.file.".config/mako/config".source = common/mako/config;
```

### Sound
In my `configuration.nix`:
```
  security.rtkit.enable = true;
  services.pipewire = {
    enable = true;
    alsa.enable = true;
    alsa.support32Bit = true;
    pulse.enable = true;
    wireplumber.enable = true;
    media-session.enable = false;
    jack.enable = true;
    systemWide = false;
  };
```

### Nextcloud
I want a purely CLI way to handle this, and a periodic sync is sufficient for me.
We do this with `systemd-timers` (Nix advises against `cron`). 
In my `home.nix`:
```
  systemd.user.services = {
    nextcloud-sync = {
      Unit = {
        Description = "Auto sync Nextcloud";
        After = "network-online.target";
      };
      Service = {
        Type = "simple";
        EnvironmentFile = "${config.home.homeDirectory}/.nextcloud.env";
        ExecStart = ''
          ${pkgs.nextcloud-client}/bin/nextcloudcmd \
          -h --non-interactive \
          --user "''${NEXTCLOUD_USER}" \
          --password "''${NEXTCLOUD_PASSWORD}" \
          ''${NEXTCLOUD_DIR} \
          ''${NEXTCLOUD_URL}
        '';
        TimeoutStopSec = "180";
        KillMode = "process";
        KillSignal = "SIGINT";
      };
      Install.WantedBy = ["multi-user.target"];
    };
  };
  systemd.user.timers = {
    nextcloud-sync = {
      Unit.Description = "Automatic sync files with Nextcloud when booted up after 5 minutes then rerun every 10 minutes";
      Timer.OnUnitActiveSec = "10min";
      Install.WantedBy = ["multi-user.target" "timers.target"];
    };
  };
  systemd.user.startServices = true;
```

## Conclusions
1. Yes setting things up from a minimal iso is harder than in Manjaro.
Although I got everything I wanted working, there were many times I felt 
like giving up. (Aside: I am *so grateful* for Manjaro, and Arch wikis).
That said, I expect this friction is a one-time cost for building familiarity with Nix, 
and well worth the benefits that come with it.
2. You bet Wayland is ready. Multi-monitor works out-of-the-box, 
all my apps support it, and things just *feel* more polished than I have ever felt with Xorg.
3. `dwl` is as awesome as I had hoped! This is my daily driver now.

## My configs
All the configs discussed (and more) are in my [nixdotfiles repo](https://gitlab.com/kdam0/dotfiles-nix).