fix formats for 20230228.4

This commit is contained in:
Edward Liu 2023-04-08 12:32:16 +08:00
parent 1f4aa672ca
commit 2deb03fd51

View File

@ -10,33 +10,19 @@
Some notes on using nix
======
Recently I started using a Mac for the first time. The biggest downside Ive
noticed so far is that the package management is much worse than on Linux.
At some point I got frustrated with homebrew because I felt like it was
spending too much time upgrading when I installed new packages, and so I
thought maybe Ill try the [nix][1] package manager!
Recently I started using a Mac for the first time. The biggest downside Ive noticed so far is that the package management is much worse than on Linux. At some point I got frustrated with homebrew because I felt like it was spending too much time upgrading when I installed new packages, and so I thought maybe Ill try the [nix][1] package manager!
nix has a reputation for being confusing (it has its whole
own programming language!), so Ive been trying to figure out how to use nix in
a way thats as simple as possible and does not involve managing any
configuration files or learning a new programming language. Heres what Ive
figured out so far! Well talk about how to:
nix has a reputation for being confusing (it has its whole own programming language!), so Ive been trying to figure out how to use nix in a way thats as simple as possible and does not involve managing any configuration files or learning a new programming language. Heres what Ive figured out so far! Well talk about how to:
- install packages with nix
- build a custom nix package for a C++ program called [paperjam][2]
- install a 5-year-old version of [hugo][3] with nix
As usual Ive probably gotten some stuff wrong in this post since Im still
pretty new to nix. Im also still not sure how much I like nix its very
confusing! But its helped me compile some software that I was struggling to
compile otherwise, and in general it seems to install things faster than
homebrew.
As usual Ive probably gotten some stuff wrong in this post since Im still pretty new to nix. Im also still not sure how much I like nix its very confusing! But its helped me compile some software that I was struggling to compile otherwise, and in general it seems to install things faster than homebrew.
#### whats interesting about nix?
People often describe nix as “declarative package management”. I dont
care that much about declarative package management, so here are two things
that I appreciate about nix:
People often describe nix as “declarative package management”. I dont care that much about declarative package management, so here are two things that I appreciate about nix:
- It provides binary packages (hosted at [https://cache.nixos.org/][4]) that you can quickly download and install
- For packages which dont have binary packages, it makes it easier to compile them
@ -44,12 +30,8 @@ that I appreciate about nix:
I think that the reason nix is good at compiling software is that:
- you can have multiple versions of the same library or program installed at a time (you could have 2 different versions of libc for instance). For example I have two versions of node on my computer right now, one at `/nix/store/4ykq0lpvmskdlhrvz1j3kwslgc6c7pnv-nodejs-16.17.1` and one at `/nix/store/5y4bd2r99zhdbir95w5pf51bwfg37bwa-nodejs-18.9.1`.
- when nix builds a package, it builds it in isolation, using only the
specific versions of its dependencies that you explicitly declared. So
theres no risk that the package secretly depends on another package on your
system that you dont know about. No more fighting with `LD_LIBRARY_PATH`!
- a lot of people have put a lot of work into writing down all of the
dependencies of packages
- when nix builds a package, it builds it in isolation, using only the specific versions of its dependencies that you explicitly declared. So theres no risk that the package secretly depends on another package on your
system that you dont know about. No more fighting with `LD_LIBRARY_PATH`! - a lot of people have put a lot of work into writing down all of the dependencies of packages
Ill give a couple of examples later in this post of two times nix made it easier for me to compile software.
@ -72,15 +54,11 @@ nix-env -iA nixpkgs.fish
This seems to just download some binaries from [https://cache.nixos.org][8] pretty simple.
Some people use nix to install their Node and Python and Ruby packages, but I havent
been doing that I just use `npm install` and `pip install` the same way I
always have.
Some people use nix to install their Node and Python and Ruby packages, but I havent been doing that I just use `npm install` and `pip install` the same way I always have.
#### some nix features Im not using
There are a bunch of nix features/tools that Im not using, but that Ill
mention. I originally thought that you _had_ to use these features to use nix,
because most of the nix tutorials Ive read talk about them. But you dont have to use them.
There are a bunch of nix features/tools that Im not using, but that Ill mention. I originally thought that you _had_ to use these features to use nix, because most of the nix tutorials Ive read talk about them. But you dont have to use them.
- NixOS (a Linux distribution)
- [nix-shell][9]
@ -88,8 +66,7 @@ because most of the nix tutorials Ive read talk about them. But you dont h
- [home-manager][11]
- [devenv.sh][12]
I wont go into these because I havent really used them and there are lots of
explanations out there.
I wont go into these because I havent really used them and there are lots of explanations out there.
#### where are nix packages defined?
@ -107,16 +84,14 @@ I found a way to search nix packages from the command line that I liked better:
#### everything is installed with symlinks
One of nixs major design choices is that there isnt one single `bin` with all
your packages, instead you use symlinks. There are a lot of layers of symlinks. A few examples of symlinks:
One of nixs major design choices is that there isnt one single `bin` with all your packages, instead you use symlinks. There are a lot of layers of symlinks. A few examples of symlinks:
- `~/.nix-profile` on my machine is (indirectly) a symlink to `/nix/var/nix/profiles/per-user/bork/profile-111-link/`
- `~/.nix-profile/bin/fish` is a symlink to `/nix/store/afkwn6k8p8g97jiqgx9nd26503s35mgi-fish-3.5.1/bin/fish`
When I install something, it creates a new `profile-112-link` directory with new symlinks and updates my `~/.nix-profile` to point to that directory.
I think this means that if I install a new version of `fish` and I dont like it, I can
easily go back just by running `nix-env --rollback` itll move me to my previous profile directory.
I think this means that if I install a new version of `fish` and I dont like it, I can easily go back just by running `nix-env --rollback` itll move me to my previous profile directory.
#### uninstalling packages doesnt delete them
@ -161,28 +136,19 @@ I havent really upgraded anything yet. I think that if something goes wrong w
nix-env --rollback
```
Someone linked me to [this post from Ian Henry][15] that
talks about some confusing problems with `nix-env --upgrade` maybe it
doesnt work the way youd expect? I guess Ill be wary around upgrades.
Someone linked me to [this post from Ian Henry][15] that talks about some confusing problems with `nix-env --upgrade` maybe it doesnt work the way youd expect? I guess Ill be wary around upgrades.
#### next goal: make a custom package of paperjam
After a few months of installing existing packages, I wanted to make a custom package with nix for a program called [paperjam][2] that wasnt already packaged.
I was actually struggling to compile `paperjam` at all even without nix because the version I had
of `libiconv` I has on my system was wrong. I thought it might be easier to
compile it with nix even though I didnt know how to make nix packages yet. And
it actually was!
I was actually struggling to compile `paperjam` at all even without nix because the version I had of `libiconv` I has on my system was wrong. I thought it might be easier to compile it with nix even though I didnt know how to make nix packages yet. And it actually was!
But figuring out how to get there was VERY confusing, so here are some notes about how I did it.
#### how to build an example package
Before I started working on my `paperjam` package, I wanted to build an example existing package just to
make sure I understood the process for building a package. I was really
struggling to figure out how to do this, but I asked in Discord and someone
explained to me how I could get a working package from [https://github.com/NixOS/nixpkgs/][13] and build it. So here
are those instructions:
Before I started working on my `paperjam` package, I wanted to build an example existing package just to make sure I understood the process for building a package. I was really struggling to figure out how to do this, but I asked in Discord and someone explained to me how I could get a working package from [https://github.com/NixOS/nixpkgs/][13] and build it. So here are those instructions:
**step 1:** Download some arbitrary package from [nixpkgs][13] on github, for example the `dash` package:
@ -190,8 +156,7 @@ are those instructions:
wget https://raw.githubusercontent.com/NixOS/nixpkgs/47993510dcb7713a29591517cb6ce682cc40f0ca/pkgs/shells/dash/default.nix -O dash.nix
```
**step 2**: Replace the first statement (`{ lib , stdenv , buildPackages , autoreconfHook , pkg-config , fetchurl , fetchpatch , libedit , runCommand , dash }:` with `with import <nixpkgs> {};` I dont know why you have to do this,
but it works.
**step 2**: Replace the first statement (`{ lib , stdenv , buildPackages , autoreconfHook , pkg-config , fetchurl , fetchpatch , libedit , runCommand , dash }:` with `with import <nixpkgs> {};` I dont know why you have to do this, but it works.
**step 3**: Run `nix-build dash.nix`
@ -207,11 +172,7 @@ Thats all! Once Id done that, I felt like I could modify the `dash` packag
`paperjam` has one dependency (`libpaper`) that also isnt packaged yet, so I needed to build `libpaper` first.
Heres `libpaper.nix`. I basically just wrote this by copying and pasting from
other packages in the [nixpkgs][13] repository.
My guess is whats happening here is that nix has some default rules for
compiling C packages (like “run `make install`”), so the `make install` happens
default and I dont need to configure it explicitly.
Heres `libpaper.nix`. I basically just wrote this by copying and pasting from other packages in the [nixpkgs][13] repository. My guess is whats happening here is that nix has some default rules for compiling C packages (like “run `make install`”), so the `make install` happens default and I dont need to configure it explicitly.
```
with import <nixpkgs> {};
@ -249,10 +210,7 @@ Next, I needed to compile `paperjam`. Heres a link to the [nix package I wrot
I set the hashes by first leaving the hash empty, then running `nix-build` to get an error message complaining about a mismatched hash. Then I copied the correct hash out of the error message.
I figured out how to set `installFlags` just by running `rg PREFIX`
in the nixpkgs repository I figured that needing to set a `PREFIX` was
pretty common and someone had probably done it before, and I was right. So I
just copied and pasted that line from another package.
I figured out how to set `installFlags` just by running `rg PREFIX` in the nixpkgs repository I figured that needing to set a `PREFIX` was pretty common and someone had probably done it before, and I was right. So I just copied and pasted that line from another package.
Then I ran:
@ -265,29 +223,17 @@ and then everything worked and I had `paperjam` installed! Hooray!
#### next goal: install a 5-year-old version of hugo
Right now I build this blog using Hugo 0.40, from 2018. I dont need any new
features so I havent felt a need to upgrade. On Linux this is easy: Hugos
releases are a static binary, so I can just download the 5-year-old binary from
the [releases page][17] and
run it. Easy!
Right now I build this blog using Hugo 0.40, from 2018. I dont need any new features so I havent felt a need to upgrade. On Linux this is easy: Hugos releases are a static binary, so I can just download the 5-year-old binary from the [releases page][17] and run it. Easy!
But on this Mac I ran into some complications. Mac hardware has changed in the
last 5 years, so the Mac Hugo binary I downloaded crashed. And when I tried to
build it from source with `go build`, that didnt work either because Go build
norms have changed in the last 5 years as well.
But on this Mac I ran into some complications. Mac hardware has changed in the last 5 years, so the Mac Hugo binary I downloaded crashed. And when I tried to build it from source with `go build`, that didnt work either because Go build norms have changed in the last 5 years as well.
I was working around this by running Hugo in a Linux docker container, but I
didnt love that: it was kind of slow and it felt silly. It shouldnt be that
hard to compile one Go program!
I was working around this by running Hugo in a Linux docker container, but I didnt love that: it was kind of slow and it felt silly. It shouldnt be that hard to compile one Go program!
Nix to the rescue! Heres what I did to install the old version of Hugo with
nix.
Nix to the rescue! Heres what I did to install the old version of Hugo with nix.
#### installing Hugo 0.40 with nix
I wanted to install Hugo 0.40 and put it in my PATH as `hugo-0.40`. Heres how
I did it. I did this in a kind of weird way, but it worked ([Searching and installing old versions of Nix packages][18]
describes a probably more normal method).
I wanted to install Hugo 0.40 and put it in my PATH as `hugo-0.40`. Heres how I did it. I did this in a kind of weird way, but it worked ([Searching and installing old versions of Nix packages][18] describes a probably more normal method).
**step 1**: Search through the nixpkgs repo to find Hugo 0.40
@ -318,33 +264,19 @@ I figured out how to run this by running `rg 'mv '` in the nixpkgs repository an
I installed into my `~/.nix-profile/bin` by running `nix-env -i -f hugo.nix`.
And it all works! I put the final `.nix` file into my own personal [nixpkgs repo][20] so that I can use it again later if I
want.
And it all works! I put the final `.nix` file into my own personal [nixpkgs repo][20] so that I can use it again later if I want.
#### reproducible builds arent magic, theyre really hard
I think its worth noting here that this `hugo.nix` file isnt magic the
reason I can easily compile Hugo 0.40 today is that many people worked for a long time to make it possible to
package that version of Hugo in a reproducible way.
I think its worth noting here that this `hugo.nix` file isnt magic the reason I can easily compile Hugo 0.40 today is that many people worked for a long time to make it possible to package that version of Hugo in a reproducible way.
#### thats all!
Installing `paperjam` and this 5-year-old version of Hugo were both
surprisingly painless and actually much easier than compiling it without nix,
because nix made it much easier for me to compile the `paperjam` package with
the right version of `libiconv`, and because someone 5 years ago had already
gone to the trouble of listing out the exact dependencies for Hugo.
Installing `paperjam` and this 5-year-old version of Hugo were both surprisingly painless and actually much easier than compiling it without nix, because nix made it much easier for me to compile the `paperjam` package with the right version of `libiconv`, and because someone 5 years ago had already gone to the trouble of listing out the exact dependencies for Hugo.
I dont have any plans to get much more complicated with nix (and its still
very possible Ill get frustrated with it and go back to homebrew!), but well
see what happens! Ive found it much easier to start in a simple way and then
start using more features if I feel the need instead of adopting a whole bunch
of complicated stuff all at once.
I dont have any plans to get much more complicated with nix (and its still very possible Ill get frustrated with it and go back to homebrew!), but well see what happens! Ive found it much easier to start in a simple way and then start using more features if I feel the need instead of adopting a whole bunch of complicated stuff all at once.
I probably wont use nix on Linux Ive always been happy enough with `apt`
(on Debian-based distros) and `pacman` (on Arch-based distros), and theyre
much less confusing. But on a Mac it seems like it might be worth it. Well
see! Its very possible in 3 months Ill get frustrated with nix and just go back to homebrew.
I probably wont use nix on Linux Ive always been happy enough with `apt` (on Debian-based distros) and `pacman` (on Arch-based distros), and theyre much less confusing. But on a Mac it seems like it might be worth it. Well see! Its very possible in 3 months Ill get frustrated with nix and just go back to homebrew.
--------------------------------------------------------------------------------