content/blog/installing_nixos_2.md (view raw)
1+++
2title = "Installing NixOS, Take 2"
3date = 2019-02-15
4updated = 2019-02-16
5[taxonomies]
6category = ["sw"]
7tags = ["os", "nixos"]
8+++
9
10This is my second take at installing NixOS, after a while being frustrated with Arch Linux and the fact that a few kernel upgrades ago, the system crashed randomly from time to time. `journalctl` did not have any helpful hints and I thought reinstalling could be worthwhile anyway.
11
12This time, I started with more knowledge! The first step is heading to the [NixOS website](https://nixos.org) and downloading their minimal installation CD for 64 bits. I didn't go with their graphical live CD, because their [installation manual](https://nixos.org/nixos/manual) is a wonderful resource that guides you nicely.
13
14Once you have downloaded their `.iso`, you should probably verify it's `sha256sum` and make sure that it matches. The easiest thing to do in my opinion is using an USB to burn the image in it. Plug it in and check its device name with `fdisk -l`. In my case, it was `/dev/sdb`, so I went ahead with it and ran `dd if=nixos.iso of=/dev/sdb status=progress`. Make sure to run `sync` once that's done.
15
16If either `dd` or `sync` seem "stuck" in the end, they are just flushing the changes to disk to make sure all is good. This is normal, and depends on your drives.
17
18Now, reboot your computer with the USB plugged in and make sure to boot into it. You should be welcome with a pretty screen. Just select the first option and wait until it logs you in as root. Once you're there you probably want to `loadkeys es` or whatever your keyboard layout is, or you will have a hard time with passwords, since the characters are all over the place.
19
20In a clean disk, you would normally create the partitions now. In my case, I already had the partitions made (100MB for the EFI system, where `/boot` lives, 40GB for the root `/` partition with my old Linux installation, and 700G for `/home`), so I didn't need to do anything here. The manual showcases `parted`, but I personally use `fdisk`, which has very helpful help I check every time I use it.
21
22**Important**: The `XY` in `/dev/sdXY` is probably different in your system! Make sure you use `fdisk -l` to see the correct letters and numbers!
23
24With the partitions ready in my UEFI system, I formatted both `/` and `/boot` just to be safe with `mkfs.ext4 -L nixos /dev/sda2` and `mkfs.fat -F 32 -n boot /dev/sda1` (remember that these are the letters and numbers used in my partition scheme). Don't worry about the warning in the second command regarding lowercase letters and Windows. It's not really an issue.
25
26Now, since we gave each partition a label, we can easily mount them through `mount /dev/disk/by-label/nixos /mnt` and, in UEFI systems, be sure to `mkdir -p /mnt/boot` and `mount /dev/disk/by-label/boot /mnt/boot`. I didn't bother setting up swap, since I have 8GB of RAM in my laptop and that's really enough for my use case.
27
28With that done, we will now ask the configuration wizard to do some work for us (in particular, generate a template) with `nixos-generate-config --root /mnt`. This generates a very well documented file that we should edit right now (and this is important!) with whatever editor you prefer. I used `vim`, but you can change it for `nano` if you prefer.
29
30On to the configuration file, we need to enable a few things, so `vim /mnt/etc/nixos/configuration.nix` and start scrolling down. We want to make sure to uncomment:
31
32```
33# We really want network!
34networking.wireless.enable = true;
35
36# This "fixes" the keyboard layout. Put the one you use.
37i18n = {
38consoleKeyMap = "es";
39}
40
41# Timezones are tricky so let's get this right.
42time.timeZone = "Europe/Madrid";
43
44# We *really* want some base packages installed, such as
45# wpa_supplicant, or we won't have a way to connect to the
46# network once we install...
47environment.systemPackages = with pkgs; [
48wpa_supplicant wget curl vim neovim cmus mpv firefox git tdesktop
49];
50
51# Printing is useful, sure, enable CUPS
52services.printing.enable = true;
53
54# We have speakers, let's make use of them.
55sound.enable = true;
56hardware.pulseaudio.enable = true;
57
58# We want the X11 windowing system enabled, in Spanish.
59services.xserver.enable = true;
60services.xserver.layout = "es";
61
62# I want a desktop manager in my laptop.
63# I personally prefer XFCE, but the manual shows plenty
64# of other options, such as Plasma, i3 WM, or whatever.
65services.xserver.desktopManager.xfce.enable = true;
66services.xserver.desktopManager.default = "xfce";
67
68# Touchpad is useful (although sometimes annoying) in a laptop
69services.xserver.libinput.enable = true;
70
71# We don't want to do everything as root!
72users.users.lonami = {
73isNormalUser = true;
74uid = 1000;
75home = "/home/lonami";
76extraGroups = [ "wheel" "networkmanager" "audio" ];
77};
78```
79
80*(Fun fact, I overlooked the configuration file until I wrote this and hadn't noticed sound/pulseaudio was there. It wasn't hard to find online how to enable it though!)*
81
82Now, let's modify `hardware-configuration.nix`. But if you have `/home` in a separate partition like me, you should run `blkid` to figure out its UUID. To avoid typing it out myself, I just ran `blkid >> /mnt/etc/nixos/hardware-configuration.nix` so that I could easily move it around with `vim`:
83
84```
85# (stuff...)
86
87fileSystems."/home" =
88{ device = "/dev/disk/by-uuid/d344c686-cae7-4dd3-840e-308eddf86608";
89fsType = "ext4";
90};
91
92# (more stuff...)
93```
94
95Note that, obviously, you should put your own partition's UUID there. Modifying the configuration is where I think the current NixOS' manual should have made more emphasis, at this step of the installation. They do detail it below, but that was already too late in my first attempt. Anyway, you can boot from the USB and run `nixos-install` as many times as you need until you get it working!
96
97But before installing, we need to configure the network since there are plenty of things to download. If you want to work from WiFi, you should first figure out the name of your network card with `ip link show`. In my case it's called `wlp3s0`. So with that knowledge we can run `wpa_supplicant -B -i wlp3s0 -c <(wpa_passphrase SSID key)`. Be sure to replace both `SSID` and `key` with the name of your network and password key, respectively. If they have spaces, surround them in quotes.
98
99Another funny pitfall was typing `wpa_supplicant` in the command above twice (instead of `wpa_passphrase`). That sure spit out a few funny errors! Once you have ran that, wait a few seconds and `ping 1.1.1.1` to make sure that you can reach the internet. If you do, `^C` and let's install NixOS!
100
101```
102nixos-install
103```
104
105Well, that was pretty painless. You can now `reboot` and enjoy your new, functional system.
106
107Afterword
108---------
109
110The process of installing NixOS was really painless once you have made sense out of what things mean. I was far more pleased this time than in my previous attempt, despite the four attempts I needed to have it up and running.
111
112However not all is so good. I'm not sure where I went wrong, but the first time I tried with `i3` instead of `xfce`, all I was welcome with was a white, small terminal in the top left corner. I even generated a configuration file with `i3-config-wizard` to make sure it could detect my Mod1/Mod4 keys (which, it did), but even after rebooting, my commands weren't responding. For example, I couldn't manage to open another terminal with `Mod1+Enter`. I'm not even sure that I was in `i3`…
113
114In my very first attempt, I pressed `Alt+F8` as suggested in the welcome message. This took me an offline copy of the manual, which is really nicely done. Funny enough, though, I couldn't exit `w3m`. Both `Q` and `B` to quit and take me back wouldn't work. Somehow, it kept throwing me back into `w3m`, so I had to forcibly shutdown.
115
116In my second attempt, I also forgot to configure network, so I had no way to download `wpa_supplicant` without having `wpa_supplicant` itself to connect my laptop to the network! So, it was important to do that through the USB before installing it (which comes with the program preinstalled), just by making sure to add it in the configuration file.
117
118Some other notes, if you can't reach the internet, don't add any DNS in `/etc/resolv.conf`. This should be done declaratively in `configuration.nix`.
119
120In the end, I spent the entire afternoon playing around with it, taking breaks and what-not. I still haven't figured out why `nvim` was printing the literal escape character when going from normal to insert mode in the `xfce4-terminal` (and other actions also made it print this "garbage" to the console), why sometimes the network can reach the internet (and only some sites!) and sometimes not, and how to setup dualboot.
121
122But despite all of this, I think it was a worth installing it again. One sure sees things from a different perspective, and gets the chance to write another blog post!
123
124If there's something I overlooked or that could be done better, or maybe you can explain it differently, please be sure to [contact me](https://lonami.dev/contact) to let me know!
125
126Update
127------
128
129Well, that was surprisingly fast feedback. Thank you very much [@bb010g](https://bb010g.keybase.pub/) for it! As they rightfully pointed out, one can avoid adding `/home` manually to `hardware-configuration.nix` if you mount it before generating the configuration files. However, the installation process doesn't need `/home` mounted, so I didn't do it.
130
131The second weird issue with `w3m` is actually a funny one. `Alt+F8` *switches to another TTY*! That's why quitting the program wouldn't do anything. You'd still be in a different TTY! Normally, this is `Ctrl+Alt+FX`, so I hadn't even thought that this is what could be happening. Anyway, the solution is not quitting the program, but rather going back to the main TTY with `Alt+F1`. You can switch back and forth all you need to consult the manual.
132
133More suggestions are having [`home-manager`](https://github.com/rycee/home-manager) manage the graphical sessions, since it should be easier to deal with than the alternatives.
134
135Despite having followed the guide and having read it over and over several times, it seems like my thoughts in this blog post may be a bit messy. So I recommend you also reading through the guide to have two versions of all this, just in case.
136
137Regarding network issues, they use `connman` so that may be worth checking out.
138
139Regarding terminal issues with `nvim` printing the literal escape character, I was told off for not having checked what my `$TERM` was. I hadn't really looked into it much myself, just complained about it here, so sorry for being annoying about that. A quick search in the `nixpkgs` repository lets us find [neovim/default.nix](https://github.com/NixOS/nixpkgs/blob/release-18.09/pkgs/applications/editors/neovim/default.nix), with version 0.3.1. Looking at [Neovim's main repository](https://github.com/neovim/neovim) we can see that this is a bit outdated, but that is fine.
140
141If only I had bothered to look at [Neovim's wiki](https://github.com/neovim/neovim/wiki/FAQ#nvim-shows-weird-symbols-2-q-when-changing-modes), (which they found through [Neovim's GitHub issues](https://github.com/neovim/neovim/issues/7749)) I would've seen that some terminals just don't support the program properly. The solution is, of course, to use a different terminal emulator with better support or to disable the `guicursor` in Neovim's config.
142
143This is a pretty good life lesson. 30 seconds of searching, maybe two minutes and a half for also checking XFCE issues, are often more than enough to troubleshoot your issues. The internet is a big place and more people have surely came across the problem before, so make sure to look online first. In my defense I'll say that it didn't bother me so much so I didn't bother looking for that soon either.