How to deal with actions on fs, when nix store is readonly?

I had a bunch of problems which seemed to be related to this issue,

  1. btrfs filesystem defragment command didn’t work, due to NixOS readonly filesystem
  2. didn’t know how can I set up a compressed with zstd btrfs filesystem in my nix config file without deleting everything
  3. some pip installs didn’t work, I assume using pip as a whole is a bad practice, but it would be nice in some cases
  4. KDE partition manager didn’t work, GParted did

So

  1. How can I make filesystem readable (I tried nix.readOnlyStore = false;)
  2. How can I fix above issues the “correct way” (specially 1 and 2)
  3. How GParted worked? And, why KDE partition manager didn’t?

To enable compression on /, using zstd and taking effect after the next build and boot, use this in /etc/nixos/configuration.nix:

  fileSystems = {
    "/".options = [ "compress=zstd" ];
  };

(I think that, depending on how your mounts/subvolumes are set up, you may need to add a line for "/nix/store".)

This configuration will get added to the configuration declared in the auto-generated hardware-configuration.nix; I believe that the compress option will be added to that file’s options list(s) when merging, as opposed to replacing it.

To actually compress what’s already in, for example, /nix/store:

  1. Note existing usage and compression with:

    # btrfs filesystem show /nix/store
    # compsize -x /nix/store
    
  2. Remount /nix/store as read-write:

    # mount /nix/store -o remount,rw

  3. Compress, using zstd:

    # btrfs filesystem defragment -r -czstd /nix/store

    (From comments near the end of the discussion on this issue.)

  4. Check new usage and compression with the commands from step 1.


KDE Partition Manager doesn’t seem to work for me in 22.11, either. The window and its UI loads, but everything is grayed out.


Apologies if reviving old threads is frowned upon here. I thought I’d add what I’ve learned so far on this as there were no other answers, and this is one of only two search results I got for my query on defragmenting/compressing here at the forum.

It’s typically enough to enable compression on your /nix/store mount (or its parent, i.e. /) and then just leave it be.

After the next staging-next merge (happens every ~2w), you’ll likely have to download your entire closure again which will be written compressed and after a gc the old uncompressed paths will be gone.

No need to mount the store read-write.

If you do need to compress it immediately though, you should simply mount the subvol containing the Nix store again somewhere else (i.e. mount mount /dev/foo -o subvol=nix /mnt/) and run defrag against the new mount. This prevents issues of bad apps (incompetent or malicious) writing to /nix during the timeframe it’d be rw otherwise.

2 Likes

Thanks!

Does that apply even if on the stable channel?

Not 100% sure how the schedule is there but it also happens on the stable channel.

1 Like

Happens every now and then, especially when an OpenSSL patch is required :slightly_smiling_face:
I’d also say: Just enable it, and wait for paths to be compressed as they come in.
If you are still on 22.11 most of you closure will change when moving to 23.05.

1 Like