Install NIX Package Manager on Alpine Linux
Table of Contents
I wanted to run programs that are challenging to port to Alpine Linux. NIX is great for this so this is how I got the nix packages running on my linux workstation.
1. NIX Package Manager Install on Alpine Linux
1.1. System Information
- alpine 3.17.1 x86-64
- linux 6.1.8-lts w/zfs
- edge, testing apk repos enabled
1.2. Preparation
apk add sudo apk add shadow apk add bash
- pkg sudo is needed; aliasing /usr/bin/doas does not work
- pkg shadow provides groupadd and related tools, needed by nix install script
- the install script might not behave with ash so install bash
1.3. Perform the sh-bang multi-user installation
sh <(curl -L https://nixos.org/nix/install) --daemon # answer no to more info # answer yes to sudo # answer yes to proceed with multi-user installation # yes to continue # ... pray ... # if successfull, acknowledge the reminder
1.4. nix rc service script
Alpine does not use systemd. Copy this file to /etc/init.d/nix-daemon and make it executable. I copied this script from the testing package in the alpine package repository.
#!/sbin/openrc-run description="Nix multi-user support daemon" command="/usr/sbin/nix-daemon" command_background="yes" pidfile="/run/$RC_SVCNAME.pid"
For some reason, the multi-user install does not install the nix-daemon binary in a system directory, instead it gets installed here:
/root/.nix-profile/bin/nix-daemon
I chose to copy this binary to /usr/sbin which seems to work.
Enable and start the service:
# run as root or sudo chmod a+rx /etc/init.d/nix-daemon cp /root/.nix-profile/bin/nix-daemon /usr/sbin rc-update add nix-daemon rc-service nix-daemon start
1.5. Post install steps
At this point, you should make sure that your userid has been added to the nixblk group. Also we need to open up the permissions on the nix-daemon socket so nixbld group members can communicate with the daemon.
Follow the instructions the script emits - run it as root the first time:
# nix installer should have emitted this text: # Alright! We're done! # Try it! Open a new terminal, and type: # nix-shell -p nix-info --run "nix-info -m"
The output should look something similar to the following:
- system: `"x8664-linux"`
- host os: `Linux 6.1.8-0-lts, Alpine Linux, noversion, nobuild`
- multi-user?: `yes`
- sandbox: `yes`
- version: `nix-env (Nix) 2.13.1`
- channels(root): `"nixpkgs"`
- nixpkgs: `/root/.nix-defexpr/channels/nixpkgs`
Now, before we try running nix as non-root user, let's add ourselves to the nixblk group and reboot. This will ensure our userid is in the nixbld group and that all running shells have picked it up. Rebooting after this will also test that our service starts correctly on a fresh boot.
sudo adduser YOURUSERID nixbld reboot (or do a safe shutdown however you usually do it)
1.6. Non root user testing
Now, let's try the first steps documentation from https://nixos.org/guides/ad-hoc-developer-environments.html as our default user.
$ hello The program ‘hello’ is currently not installed. $ nix-shell -p hello [nix-shell:~]$ hello Hello, world! [nix-shell:~]$ exit exit $ hello The program ‘hello’ is currently not installed.
Now we can try running a real application inside of nix-shell and then we will install it so it's available outside of the nix-shell application.
1.6.1. Run deno inside nix-shell
proteus:~$ nix-shell -p deno this path will be fetched (24.28 MiB download, 80.64 MiB unpacked): /nix/store/kn6c4dkql7jhh2vzdja78bs3rs59hpb2-deno-1.29.4 copying path '/nix/store/kn6c4dkql7jhh2vzdja78bs3rs59hpb2-deno-1.29.4' from 'https://cache.nixos.org'... [nix-shell:~]$ deno --version deno 1.29.4 (release, x86_64-unknown-linux-gnu) v8 10.9.194.5 typescript 4.9.4 [nix-shell:~]$ exit
1.6.2. Install deno with nix
$ nix-env --install deno installing 'deno-1.29.4' $ deno --version deno 1.29.4 (release, x86_64-unknown-linux-gnu) v8 10.9.194.5 typescript 4.9.4
This is great since deno isn't available for Alpine Linux but it does work running inside of the nix runtime/sandbox.
1.7. User Customizations for nix
You can create your own nix.conf file in ~/.config/nix/nix.conf. This file can contain a variety of settings. Here are some things I learned.
I tried to use flakes but I got an error. The error said that I needed to enable an experimental option. Specifically:
$ nix run nixpkgs#hello error: experimental Nix feature 'nix-command' is disabled; use '--extra-experimental-features nix-command' to override
n The error message and fix is for re-running the command using the CLI.
To solve this via your personal ~/.config/nix/nix.conf file, you can add this line:
extra-experimental-features = nix-command
Note that in the config file, you do not need the prefix dashes and you need to add an '=' sign. Also, there must be whitespace before and after the equals sign.
In this example, I needed to add both 'nix-command' and 'flakes' to my nix.config file which can be done over multiple lines:
extra-experimental-features = nix-command extra-experimental-features = flakes
or combined with two or more entries, space separated, on the right hand side of the assignment as user @NobbZ said above. However, they cannot be in quotes in the config file. The following also worked for me.
extra-experimental-features = nix-command flakes