my-opinion-on-nix.mdx
1 --- 2 draft: true 3 title: My opinion on Nix, The package Manager 4 date: 2021-05-13 5 description: I really like Nix, it's great and all, but I don't think I'm going to use it, <em>yet</em>. 6 tags: 7 - linux 8 - random 9 --- 10 11 [mj-link]: https://github.com/mjlbach 12 [nix-website]: https://nixos.org/ 13 [fhs-wiki]: https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard 14 [nix-basic]: https://nixos.org/guides/nix-pills/basics-of-language.html 15 [shell-intro]: https://ghedam.at/15978/an-introduction-to-nix-shell 16 [direnv-link]: https://direnv.net/ 17 [lorri-link]: https://github.com/target/lorri 18 [hm-link]: https://github.com/nix-community/home-manager 19 [nixgl-link]: https://github.com/guibou/nixGL 20 21 import Update from "~/components/Update.astro"; 22 23 # Introduction 24 25 I've been trying to dive into Nix recently and it has been pleasant. The reason is, let's say [_someone_][mj-link] mentioned it a little bit _too_ much and got me interested. 26 27 If you don't know about Nix, it's a declarative, purely functional lazily evaluated language _and_ a package manager, and there's also NixOS which is a distribution entirely based on Nix. I won't explain what they are in this post, so please refer to their [official website][nix-website]. 28 29 This post is going to be focused on my opinion about it so please take it with a grain of salt ;) 30 31 <Update date="2021-08-15"> 32 33 ~~I've decided to use Nix to manage my dotfiles and most of my packages since I moved to Fedora. I still use `dnf` for GUI packages since they don't play nicely when installed through Nix.~~ 34 35 I'm back to Arch, but I kept using Nix. 36 37 Here's my [flake.nix](https://github.com/elianiva/dotfiles/blob/master/flake.nix) in case you want it ;) 38 39 </Update> 40 41 <Update date="2021-11-24"> 42 43 I stopped using Nix 44 45 </Update> 46 47 # NixOS 48 49 My very first introduction to Nix is actually using it from NixOS. It's a very unique Linux distribution, it doesn't use [FHS][fhs-wiki] which has its pros and cons. 50 51 Imagine, your entire system is like a git repo. You configure the system declaratively using a file in `/etc/configuration.nix`. It's super easy to configure services, packages, etc. You can also _rollback_ to a previous _generation_. Similar to how you'd do `git revert`. 52 53 Because you can configure everything from `/etc/configuration.nix`, there is _too_ much abstraction. I think it's good because it simplifies things and makes it easier because everything is centralised, but it's also not good because you no longer know which configuration goes to where. 54 55 Also, you lose the ability to run a normal binary because Nix uses a non-standard filesystem structure. You can _patch_ them, but that seems like a bit of a chore to me. 56 57 # Nix, The Language 58 59 ## Purely Functional 60 61 The language is purely functional and lazily evaluated. Some people say that its syntax is quirky, but I don't really think it is. Its syntax is quite similar to Haskell. 62 63 Quick and (overly) simplified explanation is, it adheres to the concept of functional programming which I really like. In Nix, everything (well, _mostly_. You still have a primitive data type) is basically a function with exactly one argument. If you want to insert multiple arguments, you'd need to use currying. It looks something like this. 64 65 ```nix 66 # define the function 67 sum = x: y: x + y; 68 69 # call the function 70 sum 2 5 71 ``` 72 73 ..and here's an equivalent in Javascript 74 75 ```javascript 76 // define the function 77 const sum = (x) => (y) => x + y; 78 79 // call the function 80 sum(2)(5); 81 ``` 82 83 It feels easier on the eyes than the traditional `foo()` function invocation but it can also be a bit confusing at times. Learning Nix made me understand this concept, I used to be so confused why would anyone need currying and now I understand why :) 84 85 ## Lazy Evaluation 86 87 The code won't be evaluated if it's not being used. I personally think this is a cool concept. You can create an infinite list but you only take 20 of them, then it's going to be evaluated as if it's a list with a length of 20. 88 89 This concept also exists in Haskell (and probably _most_ functional language). Might be a bit confusing for someone who never used a lazily evaluated language, though. 90 91 ## Overall 92 93 I don't think the syntax is quirky, it's unique in my opinion and I like it. If you want to know more about its syntax, please refer to [this page][nix-basic] as it's the official page and it explains them very well and thoroughly! 94 95 # Nix, The Package Manager 96 97 ## The Good 98 99 One of the nicest thing out of many things about Nix package manager is you can execute an isolated shell and it can be easily reproduced. You can use `shell.nix` or `default.nix` and execute `nix-shell` on that directory. You can read [this article][shell-intro] or the [official wiki](https://nixos.wiki/wiki/Development_environment_with_nix-shell) to understand what it does 100 101 You can also execute a package temporarily using `nix-shell -p <pkgname>` without installing it. It's super cool! 102 103 It's also cross-platform, you can install it on MacOS or any GNU/Linux distribution, though I'm not sure about the non-systemd distribution. This makes it easy reproducibility on any OS -- Except Windows, that is. You can use WSL2 on Windows, however. 104 105 ## The Not-So-Good 106 107 Now, the problem with this is since every package is 'isolated', _some_ of them like `telegram-desktop` won't work properly. It couldn't pick up the system theme, resulting in an ugly cursor. I know this is a minor issue, but it bothers me more than it should lol. 108 109 Another example is `fcitx5` which is an IME that I use to type 日本語. I don't know if it's my setup, but it doesn't seem to be able to find its addons like `mozc` -- I can switch my input method, but it has no effect when I'm typing. 110 111 The absolute deal-breaker for me though, is any package that uses OpenGL can't be used outside of NixOS without some sort of [wrapper][nixgl-link]. I don't really like adding a wrapper just to achieve this. 112 113 They work flawlessly on NixOS when I tried it, but I guess it's because the entire system is _designed_ for them. 114 115 Also, when it's downloading something, there is no progress bar which makes it seems like it doesn't do anything when it's downloading a big package. I have a slow connection and I got confused when it's trying to download something big. 116 117 ## Overall 118 119 I actually enjoy writing Nix build files, it's really exciting to write them. It's also pretty satisfying when it successfully built the package. 120 121 # Other Nix-Related Tools 122 123 ## Lorri and Direnv 124 125 Other than plain Nix, I also tried [direnv][direnv-link] and [lorri][lorri-link] briefly. I think it's super useful if you have multiple projects using Nix. 126 127 Though, since I don't have a project using Nix and most of my stuff is using Node or Rust and all of them are a one-man-project -- meaning that I'm the only one who works on it, I don't think using Nix is going to bring any advantage. 128 129 ## Home Manager 130 131 I used [home-manager][hm-link] for a few days and it has been great! It's almost like `configuration.nix` for your current user instead of system-wide. 132 133 I stopped using it though because I don't use Nix _that_ often (yet) and I don't have multiple machines so using it's probably a bit overkill. 134 135 # TL;DR 136 137 So, here's a summary of what I think about Nix. Again, please take them with a grain of salt as this is my personal opinion and I might be completely wrong ;) 138 139 ## Why Nix is good 140 141 - The language is good. Yes, there are some quirks but every language have their own quirks. 142 - The package manager is great! It provides a hermetic build environment. It's super useful when you're working with many people. 143 - It can be used on multiple operating systems. This is great if you're working with someone who uses a different operating system. 144 - You became more nerdy than most people around you :p 145 146 ## Why I won't use it 147 148 - I'm not going to use it for now since I don't work collaboratively with several people (yet ;) and most of the time I code in NodeJS related stuff, Rust, and Lua, so using it won't bring any significant benefit. I think it would be beneficial when I do some serious low level programming that requires a bunch of system dependencies that needs to be easily reproducible. 149 - I already have a package manager; which is Pacman. Using both Pacman and Nix seems like a weird setup to me. If I use MacOS, I would definitely use Nix. 150 - I have a slow connection and low storage. Some packages from Pacman get duplicated in Nix; I know this is necessary for reproducibility but I'm not trying to reproduce it anywhere at the moment. 151 - I don't want to fall _too_ deep into yet another rabbit hole ;) 152 153 # Conclusion 154 155 Overall, I like Nix. I can't wait to be able to use it for a real-world situation -- As you may or may not know, I'm still in High School when I write this and there's almost no high-schooler who thinks Nix is worth investing your time into :p 156 157 Also, here's some good articles that I've read about Nix that you _might_ want to read if you're interested. 158 159 - [nix-shell with ZSH](https://msitko.pl/blog/2020/04/22/isolated-ennvironments-with-nix-shell-and-zsh.html) 160 - [How to learn Nix](https://ianthehenry.com/posts/how-to-learn-nix/) 161 - [Nix by example](https://medium.com/@MrJamesFisher/nix-by-example-a0063a1a4c55) 162 - [How to get started with Nix](http://alessandromarrella.com/fed7de08.html) 163 - [Using Nix in my development environment](https://ejpcmac.net/blog/about-using-nix-in-my-development-workflow/) 164 165 Anyway, thanks for reading my post, and have a wonderful day! :)