treehouse/content/programming/blog/nix.tree

89 lines
4.3 KiB
Plaintext
Raw Permalink Normal View History

2024-07-10 15:14:53 +02:00
%% title = "derive me some Nix"
2024-07-17 21:37:01 +02:00
% id = "01J31356Q1A4Z54F3G7THB1C81"
2024-07-10 15:14:53 +02:00
- chances are you've heard about Nix or NixOS from somewhere and would like to learn more about it.
2024-07-17 21:37:01 +02:00
% id = "01J31356Q1AQE7ZBE2P7FG03G4"
2024-07-10 15:14:53 +02:00
- if that is the case, welcome to the club!
we're pretty much in the same bucket.
I literally just started using Nix a couple days ago, and I found the documentation *really* unclear.
so here's my shot at a more pedagogical approach.
2024-07-17 21:37:01 +02:00
% id = "01J31356Q1N80MYK77MP3BMJ1E"
2024-07-10 15:14:53 +02:00
- if that's not the case - Nix is a package manager and system configuration language.
basically, you specify how you want your system to look using config files stored in one place, and Nix _derives_ the system from that config file for you!
2024-07-17 21:37:01 +02:00
% id = "01J31356Q13VHG7EN0Q24A6GSM"
2024-07-10 15:14:53 +02:00
- I emphasized the word _derives_, because _derivation_ is the fundamental operation of Nix the package manager.
your entire config is specified declaratively, and the entire folder structure and all configuration files are _derived_ from that config.
but we'll get to that shortly.
2024-07-17 21:37:01 +02:00
% id = "01J31356Q1G1X5X9Q7BPDT41R5"
2024-07-10 15:14:53 +02:00
- people seem to be excited about Nix for many reasons.
I'm excited because it helps me achieve *online independence.*
2024-07-17 21:37:01 +02:00
% id = "01J31356Q12T9Z1WE300PD7J79"
2024-07-10 15:14:53 +02:00
- did you know that since last weekend (since this was written) this website runs on NixOS?
2024-07-17 21:37:01 +02:00
% id = "01J31356Q1KM0TT28D4PHZTAR9"
2024-07-10 15:14:53 +02:00
- being able to take your VPS's configuration elsewhere and let Nix cook your system up automatically is extremely cool.
it's something that's always frustrated me with how Linux software is configured -
lots of little configuration files scattered across `/etc` and sometimes other directories, which ends up being extremely hard to replicate if you ever want to switch server providers.
2024-07-17 21:37:01 +02:00
% id = "01J31356Q1D0SJPJB1V4MYPNX9"
2024-07-10 15:14:53 +02:00
- *and* your system config will end up identical to the one you had before!
isn't that awesome?
2024-07-17 21:37:01 +02:00
% id = "01J31356Q1BNSAF75KDE4PN2PJ"
2024-07-10 15:14:53 +02:00
- word of warning though: I wouldn't recommend running NixOS on a home PC.
2024-07-17 21:37:01 +02:00
% id = "01J31356Q1C13GSD9KSXETWW1B"
2024-07-10 15:14:53 +02:00
- I use :btw: on my home PC for a very simple reason.
it's just _really darn simple_ to change anything about my system.
2024-07-17 21:37:01 +02:00
% id = "01J31356Q1JZ13FY2XAVVKAN97"
2024-07-10 15:14:53 +02:00
- need to install a new package? `pacman -S`.
2024-07-17 21:37:01 +02:00
% id = "01J31356Q1VJ0HFQYVS1AE7CZB"
2024-07-10 15:14:53 +02:00
- need to edit system config?
well first of all no you don't, because other than fundamental config you change during installation, we both know Arch works pretty much works out of the box on your PC,
but even if you _do_ end up needing to change some config for a workaround, it's generally only because you need a _workaround_ - which may not be needed by the time you reinstall your system.
2024-07-17 21:37:01 +02:00
% id = "01J31356Q162ACZG4S6M9YNQX2"
2024-07-10 15:14:53 +02:00
- and reinstall your OS pretty rarely.
2024-07-17 21:37:01 +02:00
% id = "01J31356Q1ZXDA6QX5P978FTPS"
2024-07-10 15:14:53 +02:00
- most of your _important_ configuration is stored in `$HOME` y'know, and that's easy to keep between reinstalls - you have a separate `/home` partition after all.
2024-07-17 21:37:01 +02:00
% id = "01J31356Q1KYMMG64D3TNXH3C5"
2024-07-10 15:14:53 +02:00
- :btw: is also Really Darn Fast.
I can't say that about NixOS.
the package manager is excruciatingly slow to update your system config.
the initial install took _really_ long for how barebones of a system I specified in my config.
2024-07-17 21:37:01 +02:00
% id = "01J31356Q1CA21R7GM9MXGQ7Q4"
2024-07-10 15:14:53 +02:00
- let's derive us a system! as I mentioned before, everything in Nix is achieved by means of _derivations_.
2024-07-17 21:37:01 +02:00
% id = "01J31356Q191D3J645CT75YFEJ"
2024-07-10 15:14:53 +02:00
- from a programming perspective, a derivation is simply a function `options -> path`, where `options` is an attribute set specifying how the output `path` should be derived.
2024-07-17 21:37:01 +02:00
% id = "01J31356Q1PRWPYD0EGGY1ZK3W"
2024-07-10 15:14:53 +02:00
- and that's *it*.
2024-07-17 21:37:01 +02:00
% id = "01J31356Q1445NB18CNBE91CZP"
2024-07-10 15:14:53 +02:00
- suppose you'd like to download a file from the Internet.
that's what the `pkgs.fetchurl` function is for.
```nix
# This expression results in a path to the downloaded file.
pkgs.fetchurl {
url = "https://liquidex.house";
# As long as you fill in the hash, which I don't know...
# Nix will tell you what it should be though!
hash = "";
}
```
2024-07-17 21:37:01 +02:00
% id = "01J31356Q1HEW2HCTG27BDFJM2"
2024-07-10 15:14:53 +02:00
- but where does `pkgs` even come from?