NixOS Pins and Needles

Three terminals arranged on a NixOS desktop
workspace
A terminal desktop workspace on NixOS.

NixOS is a Linux distribution built around the nix package manager. Since writing my first article on NixOS, it has quickly become my personal OS (Operating System) of choice for desktops, servers, and Replaced my pfSense and OPNsense routers with NixOS. The transition process to become almost as Good old cowboy style system administration. as my imperative environments has been marred by a lot of pins and needles on the usability front, but it has been worth it.

For context, you can use a single configuration.nix to build machines which is super convenient, but my inventory is diverse and has at least five machines. Configuring these machines to play nice together ultimately means having to control complexity — and that’s not always fun.

This is a fairly opinionated and under the hood take on using NixOS, you shouldn’t take any of this as advice — I’m not a functional or LISP programming expert. Lasciate ogni speranza, voi ch’entrate.

Testing Single Files Quickly

My inventory includes some low end hardware so rebuilding a small subset of machines is What software today isn’t slow? Fast feedback loops are hard to create nowadays. in NixOS. This hacky script of mine dry builds single files in isolation and foregoes the need of rebuilding entire configuration sets (with some trade–offs), saving precious time. Before, I’d usually have to wait for a complete nixos-rebuild or nixops evaluation, only to discover missing imports or other syntax errors. Single file evaluation allows working on small sections of a larger set of configurations quickly without rebuilding or activating machines.

Using my hacky script nixos-test to quickly evaluate small files and even a full configuration.

GNU Guix (as in geeks) is currently superior to NixOS in this regard with commands like guix system that provide this type of fast inspection. If Guix is interesting to you, Emacs and Guile Scheme. out this cool video introduction.

Searching For Packages and Files

In most Linux distributions you can search to see which package owns a file. For example, on Arch Linux you would run pacman -Fy <file> to search the Random Aside: It appears that Valve’s Steam Deck is based on Arch Linux. That’s interesting. database for a package that contains the <file> you’re seeking. This is great when compiling or writing a program. On NixOS, my preference is to use nix-locate to find packages and files. NixOS package search and naming conventions can be unintuitive. To ease my pain, the ideas of this shell script are adapted for my specific purposes. My hacky version sets up an alternate home environment, drops the desired program into a temporary nix-shell with fish and prints some useful paths afterwards.

Poking around VLC’s package files.

Running Configurations

NixOS comes with powerful virtual machine integration testing modules. I’m a bit more lazy and like to jump directly into a virtual machine from my shell with QEMU (sometimes called Quick Emulator) to try out a configuration. The nixos-generate command generates different system output formats for a specified configuration. The ISO Optical disc image. is my favorite. Often times I’ll spin up a system in the cloud as a read only ISO.

shell
$ nixos-generate --format iso --configuration server.nix --option builders ''
$ qemu-system-x86_64 -nographic -enable-kvm -m 1024 -cdrom nixos.iso
Trying out a configuration with a virtual machine.

DRY is Evil Incarnate

The functional ecosystem has taught me that I’m not that smart anyway. programming isn’t worth the pain. Don’t repeat yourself (DRY) is the devil incarnate in my book, especially in a functional programming context. After throwing away my first directory structure, I’ve since implemented what may be a kind of naive stratified design approach to prevent myself from wasting Sometimes coming back to my module configurations leave me baffled. pulling useful logic out of deep abstractions or forcing my poorly crafted NixOS modules to work for a purpose that it was not designed for.

Nixpkgs, the NixOS implementation, lets you spin up system abstractions in your sleep and while this is good for writing packages The domain specific tooling of NixOS and Guix is amazing for writing massive amounts of system packages quickly. if your hastily made abstractions are poor — you’ll be banished to decomposing the world. My self made rule now is that anything that does not require imports and does something useful is a basic knowledge piece. Every singular piece of knowledge is saved, and whether used or not can be combined to create higher functionality independent of itself. These knowledge pieces should be easy to change, check, and reuse. For that simple reason, duplication is good, really really good, and as a bonus — sharing working snippets with others is easier.

WET: Write everything twice.

Modules Should Do One Thing

Or rather — it seems useful to distinguish between lower level modules (modules containing systemd modules only) and higher level modules (modules containing multiple modules). The high level interfacing inside a module is often too hard to pick apart. Situations can arise where you try to do something with a module that is out of its scope. The reality is that a lot of NixOS modules have two or more smaller modules struggling to get out. Sometimes you’ll have to reduce upstream modules down to its basics to get what you want done — this can be painful.

My use cases typically require running multiple copies of the same module. The easiest way to achieve this is to keep custom made modules as small as possible (just a systemd service and basic configuration). It’s impossible to satisfy all use cases so everything else ends up as a test case of combinations that can be used as higher level modules. “Lower” level modules are easier to understand and manipulate too, since they are just an indirect version of plain systemd files.

The lib.attrsets.mapAttrs' function is great for duplicating systemd services.

Separate System and User Environments

As a user, if I can avoid a nixos-rebuild, nixops deploy, sudo, or doas — sign me up. My user packages are split from the system side with config.nix abuse and nix-env. The nix community’s home manager is nice, but avoided because the less dependencies the better when connecting my home folder to another Linux distribution. My programming style incorporates the Unix shell mostly as an integrated development environment (IDE), so my dotfiles and default presets for linting and such are all that’s necessary. The new nix flake feature puts in the guard rails for proper reproducibility, but I don’t think I’ll be using that for some time.

Imperative to Declarative User Experience

Once in a while I’ll take a look at the discussions on social media around the NixOS, Guix, and functional operating system community. Recent discussions seem to center around the lack of popularity of NixOS and GNU Guix and their complexity.

The unpopularity of NixOS and GNU Guix sounds about right. Functional Linux distributions will not become generally popular anytime Sometimes getting popular too fast has more drawbacks than benefits anyway. due to misaligned developer and user experience expectations. Popularity may be limited to self–hosting, developer operations, and system administration circles.

I’m fairly certain that nixos-generate-config is the command with the best user experience (UX) on the whole system — everything else is pins and needles. NixOS needs more imperative to declarative End users don’t want long form “documentation” — what they really want is either copy and paste style short documentation, or imperative commands that generate plug and play configuration. In the end, as the joke goes — users are programmers who believe anything you say. The NixOS install procedure achieves this workflow with two separate commands. The first command generates a declarative configuration and the second implements it. If distribution installation speed runs were a thing, then NixOS would have a leg up especially if a configuration.nix is baked into a custom installer with automatic formatting options like fileSystems.<name>.autoFormat.

shell
$ nixos-generate-config --root /mnt
$ nixos-install

In my experience, a casual NixOS user doesn’t even want to open the configuration.nix to manually write stuff and connect modules together unless absolutely necessary. In their world — configuration blocks should simply be “generated” so that they can just copy and paste things.

“Why can’t it just generate the configs like it did to install my system?” A casual user

Docker, Kubernetes, Tailwind, and React all have the same Synergy — but I like to call it copy and paste–ability. Applications with high copy and paste–ability are always popular. incentives that ensure popularity. It’s not necessarily the underlying technology that makes a software ecosystem popular or “good”, but the ease of the higher level tooling to create, share, and copy from a near endless library of pre–defined documents, templates and components. In short, it’s very little effort for great reward — that’s the nature of the beast. Most users abhor starting from scratch and because of that it’s sort of easy to anticipate what will become popular — NixOS and GNU Guix are not there yet. Searching the NixOS options index to build up configuration is almost like starting from scratch in a docker compose.

Conclusion

Future iterations of NixOS, Guix and its derivatives can only get more robust. Unchanging state. may be rejected in most real world applications, but Describing what a result should be. will definitely become the name of the game. Describing how you want to get to a result. styles will undoubtedly always be around.

If easily composed and shared declarative tooling (infrastructure as code) becomes the dominant paradigm in user space, then issues in the Linux desktop space can be resolved elegantly by lassoing the majority of desktop configuration types and quirks. The NixOS hardware quirks repository is brilliant.

Interestingly enough, the nixpkgs repository is a very Hosted on GitHub. way of getting a general overview of the current state of the upstream software development world. Every dirty hack, every non–standard project setup, every network call during installation — the sandbox sees it all.

Finally, The nix language is just great in my opinion, without functions, it reads like JSON (JavaScript Object Notation) with comments minus the dangling comma shenanigans. It can be used as metadata or as a language. If I could ever manage to get my NixOS configuration under control (specifically secrets and metadata), I’d open source it, but for the time being, I just don’t have the time.

22 November 2021 — Written
22 November 2021 — Updated
Thedro Neely — Creator
nixos-pins-and-needles.md — Article

More Content

Openring

Web Ring

Comments

References

  1. https://thedroneely.com/git/
  2. https://thedroneely.com/
  3. https://thedroneely.com/posts/
  4. https://thedroneely.com/projects/
  5. https://thedroneely.com/about/
  6. https://thedroneely.com/contact/
  7. https://thedroneely.com/abstracts/
  8. https://ko-fi.com/thedroneely
  9. https://thedroneely.com/tags/nix/
  10. https://thedroneely.com/posts/nixos-pins-and-needles/#isso-thread
  11. https://thedroneely.com/posts/rss.xml
  12. https://thedroneely.com/images/nixos-pins-and-needles.png
  13. https://nixos.org/
  14. https://en.wikipedia.org/wiki/Linux_distribution
  15. https://www.thedroneely.com/posts/running-nixos-linux-containers/
  16. https://github.com/pfsense/pfsense#overview
  17. https://github.com/opnsense/core#opnsense-gui-and-system-management
  18. https://www.thedroneely.com/posts/nixops-towards-the-final-frontier#nixops-deployment-and-commands
  19. https://en.wikipedia.org/wiki/Functional_programming
  20. https://en.wikipedia.org/wiki/Lisp_%28programming_language%29
  21. https://thedroneely.com/posts/nixos-pins-and-needles/#testing-single-files-quickly
  22. https://github.com/tdro/dotfiles/blob/2eb824499ed7b4d76b29e5068c0d4e0c2f83adaa/.local/bin/nixos-test#L1
  23. https://www.mankier.com/8/nixos-rebuild
  24. https://github.com/NixOS/nixops/blob/3128b4ca31fe1e7930ce67a115eb131aa1b0b57d/doc/manual/overview.rst#overview
  25. https://thedroneely.com/videos/nixos-pins-and-needles-nixos-test.mp4
  26. https://guix.gnu.org/en/about/
  27. https://guix.gnu.org/manual/en/html_node/Invoking-guix-system.html
  28. https://www.gnu.org/software/emacs/
  29. https://www.gnu.org/software/guile/
  30. https://odysee.com/@SystemCrafters:e/5-reasons-to-try-guix-in-2022:f
  31. https://thedroneely.com/posts/nixos-pins-and-needles/#searching-for-packages-and-files
  32. https://wiki.archlinux.org/title/Arch_Linux
  33. https://www.steamdeck.com/en/tech#specs
  34. https://archlinux.org/
  35. https://www.thedroneely.com/posts/installing-isso-from-source/
  36. https://github.com/bennofs/nix-index#readme
  37. https://search.nixos.org/packages?channel=unstable&from=0&size=50&sort=relevance&type=packages&query=git
  38. https://github.com/Shopify/comma/blob/85f8d8dfe5c1d773ab2e0b1768d5004b515adc81/%2C#L1
  39. https://github.com/tdro/dotfiles/blob/3a733d76f1155a04461e31e7ff6270256f6a335a/.local/bin/%2C#L1
  40. https://nixos.org/manual/nix/unstable/command-ref/nix-shell.html
  41. https://fishshell.com/
  42. https://thedroneely.com/videos/nixos-pins-and-needles-package-searching.mp4
  43. https://www.videolan.org/vlc/
  44. https://thedroneely.com/posts/nixos-pins-and-needles/#running-configurations
  45. https://nix.dev/tutorials/integration-testing-using-virtual-machines
  46. https://www.qemu.org/
  47. https://www.mankier.com/8/nixos-generate-config
  48. https://en.wikipedia.org/wiki/Optical_disc_image
  49. https://thedroneely.com/posts/nixos-pins-and-needles/#code-block-a47c622
  50. https://thedroneely.com/videos/nixos-pins-and-needles-running-configurations.mp4
  51. https://thedroneely.com/posts/nixos-pins-and-needles/#dry-is-evil-incarnate
  52. https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
  53. https://www.thedroneely.com/posts/nixops-towards-the-final-frontier#directory-structure
  54. https://www.youtube.com/watch?v=GbZpTHg0KfQ
  55. https://www.thedroneely.com/posts/writing-nixos-modules-and-switching-to-cgit#module-interface
  56. https://nixos.wiki/wiki/Module
  57. https://github.com/NixOS/nixpkgs
  58. https://thedroneely.com/videos/nixos-pins-and-needles-no-dry.mp4
  59. https://thedroneely.com/posts/nixos-pins-and-needles/#modules-should-do-one-thing
  60. https://wiki.archlinux.org/title/systemd
  61. https://thedroneely.com/videos/nixos-pins-and-needles-modules.mp4
  62. https://thedroneely.com/posts/nixos-pins-and-needles/#separate-system-and-user-environments
  63. https://man.archlinux.org/man/sudo.8.en
  64. https://man.archlinux.org/man/doas.1.en
  65. https://www.thedroneely.com/git/thedroneely/dotfiles/tree/.config/nixpkgs/config.nix#n1
  66. https://nixos.org/manual/nix/unstable/command-ref/nix-env.html
  67. https://github.com/nix-community/home-manager#readme
  68. https://en.wikipedia.org/wiki/Unix_shell
  69. https://en.wikipedia.org/wiki/Integrated_development_environment
  70. https://www.thedroneely.com/git/thedroneely/dotfiles/tree/
  71. https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html#flake-format
  72. https://thedroneely.com/posts/nixos-pins-and-needles/#imperative-to-declarative-user-experience
  73. https://old.reddit.com/r/linuxquestions/comments/qqh55d/why_arent_systems_like_nixos_and_gnu_guix_more/
  74. https://en.wikipedia.org/wiki/DevOps
  75. https://thedroneely.com/posts/nixos-pins-and-needles/#code-block-209833e
  76. https://github.com/docker/compose#readme
  77. https://github.com/kubernetes/kubernetes#readme
  78. https://github.com/tailwindlabs/tailwindcss#readme
  79. https://github.com/facebook/react#readme
  80. https://search.nixos.org/options
  81. https://thedroneely.com/posts/nixos-pins-and-needles/#conclusion
  82. https://github.com/NixOS/nixos-hardware
  83. https://github.com/NixOS/nixpkgs/issues
  84. https://www.json.org/json-en.html
  85. https://www.thedroneely.com/posts/nixops-towards-the-final-frontier#nixops-and-nix-channel
  86. https://www.thedroneely.com/posts/nixos-pins-and-needles.md
  87. https://thedroneely.com/posts/automate-vultr-snapshots-using-bash/
  88. https://thedroneely.com/archives/abstracts/
  89. https://thedroneely.com/archives/posts/
  90. https://git.sr.ht/~sircmpwn/openring
  91. https://drewdevault.com/2022/11/12/In-praise-of-Plan-9.html
  92. https://drewdevault.com/
  93. https://mxb.dev/blog/the-indieweb-for-everyone/
  94. https://mxb.dev/
  95. https://www.taniarascia.com/simplifying-drag-and-drop/
  96. https://www.taniarascia.com/
  97. https://thedroneely.com/posts/nixos-pins-and-needles#isso-thread
  98. https://thedroneely.com/posts/nixos-pins-and-needles#testing-single-files-quickly
  99. https://thedroneely.com/posts/nixos-pins-and-needles#searching-for-packages-and-files
  100. https://thedroneely.com/posts/nixos-pins-and-needles#running-configurations
  101. https://thedroneely.com/posts/nixos-pins-and-needles#code-block-a47c622
  102. https://thedroneely.com/posts/nixos-pins-and-needles#dry-is-evil-incarnate
  103. https://thedroneely.com/posts/nixos-pins-and-needles#modules-should-do-one-thing
  104. https://thedroneely.com/posts/nixos-pins-and-needles#separate-system-and-user-environments
  105. https://thedroneely.com/posts/nixos-pins-and-needles#imperative-to-declarative-user-experience
  106. https://thedroneely.com/posts/nixos-pins-and-needles#code-block-209833e
  107. https://thedroneely.com/posts/nixos-pins-and-needles#conclusion
  108. https://thedroneely.com/posts/generating-archive-pages-with-hugo/
  109. https://thedroneely.com/posts/lets-customize-gitea/
  110. https://thedroneely.com/abstracts/the-myth-of-the-rational-voter/
  111. https://thedroneely.com/posts/declarative-user-package-management-in-nixos/
  112. https://thedroneely.com/posts/writing-with-vale/
  113. https://thedroneely.com/posts/tweaking-goaccess-for-analytics/
  114. https://drewdevault.com/2022/09/16/Open-source-matters.html
  115. https://mxb.dev/blog/make-free-stuff/
  116. https://thedroneely.com/sitemap.xml
  117. https://thedroneely.com/index.json
  118. https://thedroneely.com/resume/
  119. https://gitlab.com/tdro
  120. https://github.com/tdro
  121. https://codeberg.org/tdro
  122. https://thedroneely.com/analytics
  123. https://thedroneely.com/posts/nixos-pins-and-needles#
  124. https://creativecommons.org/licenses/by-sa/2.0/
  125. https://thedroneely.com/git/thedroneely/thedroneely.com
  126. https://opensource.org/licenses/GPL-3.0
  127. https://www.thedroneely.com/
  128. https://thedroneely.com/posts/nixos-pins-and-needles/#