❄️ Installing the nix package manager on SailfishOS

Hi sailors,

The nix package manager can be installed on SailfishOS and serve as another vast source of applications (primarily cli programs). You can browse nixpkgs if it contains a program you need (it probably will).

Today, I successfully installed nix on my Xperia 10iii with SailfishOS 4.4.0.72 Vanha Rauma (I know I should update, but my root partition is full and other stuff prevents me from updating right now…)

There is only one problem on SailfishOS that’s in the way: the comically small root partition. nix needs the directory /nix in the root of the file system. It is also notoriously space-hungry (think gigabytes, depending on what you do). While it is possible to enlarge the root partion, this might not be for everyone. Unfortunately, the /nix dir must also not be a symlink to somewhere else. There is another way though, and that is to mount something unter /nix that’s stored somewhere else. The least disruptive approach is to just make a filesystem on a single file and mount that under /nix.

Note that you need to have sudo set up, something along this should do: devel-su;pkcon install sudo, then add or uncomment the line %wheel ALL=(ALL) ALL after running visudo, add yourself to the wheel group with usermod -aG wheel defaultuser, then restart):

Here are the steps I took:

Prepare an image file for the Nix Store

# Make the 'truncate' command available
sudo pkcon remove busybox-symlinks-coreutils
sudo pkcon install gnu-coreutils

# create an empty file to hold the /nix filesystem tree
truncate -s 5G ~/.nix-store-image

Here I created it in the home directory, but you may well use your SD card (e.g. /run/media/defaultuser/*/.nix-store-image) instead. Be aware that the SD card might not always be mounted.

The 5 gigabyte (-s 5G) size might be too small. Adjust it to your needs and space. For me, /nix was 700M large after installation, but 1G was too small (df -h).

# make a filesystem on our image file
sudo mkfs -t ext4 ~/.nix-store-image

We can now mount the image under /nix:

# create the /nix folder
sudo mkdir /nix

# mount the image under /nix
sudo mount ~/.nix-store-image /nix

# make /nix belong to the defaultuser
sudo chown -R defaultuser:defaultuser /nix

Note: This mounting is not permanent and does not survive reboots. For this, it would need to be added to /etc/fstab or managed by systemd, but I haven’t set that up yet. For now, you will need to reexecute this after a reboot.

Installing Nix

We can now install nix in single-user mode according to the official instructions:

sh <(curl -L https://nixos.org/nix/install) --no-daemon

This should only take a minute.

After reopening the terminal, you should now have the nix command available in your shell:

> nix-shell -p cowsay --run 'cowsay Yay, nix on SailfishOS!'
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = (unset),
	LANG = "de_DE.utf8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
 _________________________ 
< Yay, nix on SailfishOS! >
 ------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

There seems to be some locale issues though, setting LC_ALL=C.UTF-8 fixes it temporarily:

> LC_ALL=C.UTF-8 nix-shell -p cowsay --run 'cowsay Yay, nix on SailfishOS!'
 _________________________ 
< Yay, nix on SailfishOS! >
 ------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

:inbox_tray: Using Nix

:warning: Nix may download a large amount (gigabytes) of data depending on what you install. Keep that in mind if you’re on mobile data!

To install packages, first search for the package name on https://search.nixos.org, then:

# Install tmux
nix-env -iA nixpkgs.tmux

# remove tmux again
nix-env -e tmux

# be dropped into a temporary shell with 'cowsay' available
nix-shell -p cowsay

# make some room in /nix
nix-collect-garbage # this removes nix-shell temporary stuff and keeps nix-env installed stuff
nix-channel --update # repopulate nixpkgs (necessary for some reason)

# If you run into weird errors like 'has bad meta.outputsToInstall', run the following and reinstall the last things you did
nix-env --rollback

If you messed something up, you can always just re-run the instsaller:

# reinstall nix again if something is missing
sh <(curl -L https://nixos.org/nix/install) --no-daemon

:snowflake: Flakes

Many online instructions for nix require you to have flakes enabled, this is how you enable them:

mkdir -p ~/.config/nix
echo 'experimental-features = nix-command flakes' >>  ~/.config/nix/nix.conf

:wastebasket: Uninstalling nix again

# unmount /nix if necessary
sudo umount /nix

# remove the /nix directory
sudo rm -rf /nix

# remove your image file
sudo rm ~/.nix-store-image

Then have a look through your .bash_profile or other shell startup config and remove nix-related stuff. But you can also leave that stuff in, it shouldn’t be harmful.

Cheers,

Yann (@nobodyinperson)

7 Likes

:closed_lock_with_key: Getting an up-to-date GPG with Nix to get pass to work

# install tools with nix
nix-env -iA nixpkgs.gnupg nixpkgs.pinentry-curses nixpkgs.pass

# configure pinentry
mkdir -p ~/.gnupg

# Tell GPG where to find pinentry (why is this necessary??)
echo "pinentry-program $(which pinentry-curses)" >> ~/.gnupg/gpg-agent.conf 

# kill any gpg agents to reload config
pkill gpg-agent 

# still some locale issues, need to set LC_ALL to something UTF-8
LC_ALL=C.UTF-8 pass mypassword
# properly asks for GPG password with pinentry in the terminal
3 Likes

Hi! Where did you get this program from? If I copy paste your command, I always get [defaultuser@NSC-Handy ~]$ truncate -s 5G ~/.nix-store-image
-bash: truncate: not found

Thanks for the hint. It’s in the package gnu-coreutils, which you can install with sudo pkcon install gnu-coreutils. I’ll update the instructions.

Hi! Thanks for the quick reply!
Unfortunately, it seems this package conflicts with busybox-symlinks-coreutils-1.34.1+git2-1.8.1.jolla.aarch64.
Is it safe to just uninstall that package?

Yes it is safe to remove. One of the two you’ll want to have installed, though :slight_smile:

Please explain me, what is the benefit of having NIX on Sailfish because right now I see only cons.

1 Like

Maybe the ability to install more packages ¯_(ツ)_/¯

1 Like

Interesting to see someone experiment with Nix on SFOS. Thanks for sharing this @nobodyinperson!

Have you heard about Determinate System’s Nix Installer? It’s more flexible and maybe simplifies the experience for you:

for the people unfamiliar with Nix/NixOS I think it would be worth mentioning that Nix’s package store can easily take up a few gigabytes in size (and pulling in certain packages will eat up mobile data fast). That’s sort of a catch on mobile.
it depends on the number of packages and their depemdencies, of course.

then NIX is bad idea for that. NIX is just packing system and we already have one, it’s zypper which is much less complicated and uses way more disk space than nix.

You might be able to install packages that otherwise wouldn’t be available on SailfishOS. I have the idea of installing ProtonMail Bridge through Nix, for example. This app won’t run on SailfishOS (apparently due to missing dependencies?)

1 Like

it’s enough to just prepare a spec file for it for sfos.

@lolek If you don’t see the gigantic benefits of having nix on SailfishOS, then I guess it is not for you, because you haven’t yet run into a situation where you needed it.

Sure, there is pkcon and zypper on SailfishOS. But good luck installing anything recent or custom with it. The jolla repos contain what’s necessary to run the OS, not more. Many of the core tools are outdated. GnuPG is extremely out of date, due to licensing issues IIUC. Want to use tmux? You have to use some random build off OpenRepos that doesn’t work. Or breaks after the next update. Same for all the Python stuff. Same for other shells like fish. The list is infinite, as there is a quasi infinite amount of new software being made that’s just not in the jolla repos or openrepos. Packaging dependencies and software for OpenRepos or elsewhere is very tedious in my experience. And outdated fast.

With nix, there’s a gigantic community packaging pretty much everything. You don’t have to do it. Nix packages will just work across SailfishOS updates. They are self-contained. With nix you can get proper up to date encryption tools (see my GPG example above).

For me, having a Linux phone is about sharing workflows and tools across machines. I do time tracking from the terminal with timewarrior and can amend it from the phone. I manage my finances with hledger and can emergency-analyse it on the phone. I sync files with git-annex and can have my phone be part of it. The list goes on. All of this is so much easier now with nix. I had to package or build everything myself before. Now I don’t and it’s awesome. :smiley:

7 Likes

Thanks for the hints. I added a warning about the download size to the post.

As for the Determinante Systems installer I don’t think that it’ll bring much value here (though I haven’t tried). It can’t work around the small root partition problem. And here, we’re using the single-user installation without the daemon and builder users etc. This is ideal for this effectively single-user SailfishOS and also easy to remove. No real need for the new installer’s fancy install receipt to make sure everything can be removed cleanly again. But I’d be happy to hear what your experiences with it are on SailfishOS.

Sadly in my life I had the oportunity to work with nix and nixos and honestly I see literaly zero benefits and more negative problems with it on SailfshOS. One of the biggest is the space requirements, second is the learning curve for newcomers.

Well then, easy solution for you: don’t use nix on SailfishOS. :smiley:

2 Likes

Wait, really? Last time, I tried to build ProtonMail Bridge from source and failed because some dependencies weren’t available on Sailfish.

Hi,

I am interested in this! Did you manage to get ProtonMail Bridge working with Nix?

I did! But something I did with Nix messed up my sytem (bootloop), so I undid it again.

But I had the idea of installing the Nix packagr manager in an LXC container and running Bridge from there which now works perfectly!

Really? :astonished: I wonder how that is possible. At least from my instructions here I don’t see a possibility that could affect base SFOS, let alone causing a boot loop. If you remeber what caused it, I can add it as a warning to the post.