nix-community / impermanence

Modules to help you handle persistent state on systems with ephemeral root storage [maintainer=@talyz]
MIT License
1.01k stars 76 forks source link

nixos: Files mounted before fstab mounts (directories) #149

Closed Erethon closed 8 months ago

Erethon commented 8 months ago

Hello, let me preface this by saying that I'm completely new to NixOS, so I might have misunderstood how impermanence is supposed to work.

On a fresh NixOS install (23.05), I've got /home/dgrig mounted as a tmpfs via:

  fileSystems."/home/dgrig" =
    { device = "none";
      fsType = "tmpfs";
      options = ["size=4G" "mode=777"];
    };

Then via impermanence with the nixos module I've setup some directories and files:

  environment.persistence."/persistent" = {
    users.dgrig = {
       directories = [
         "Code"
         "Documents"
         "Vault"
         "Downloads"
       ];
       files = [
         ".tmux.conf"
         ".zshrc"
         ".gitconfig"
        ".histfile"
        "nonexistant"
       ];
    };
  };

Running this via nixos-rebuild switch works fine, but when rebooting the system only the directories are present and not the files.

I've tracked this down to the order we run the systemd jobs. We first run the persist--persistent-X.services, so the files get mounted, for example /home/dgrig/.zshrc. After that the systemd-fstab-generator mounts run and we get /home/dgrig mounted, which "removes/overwrites" the files, The directories are also mounted at the same time. Here's a SVG showing the systemd boot order of the system.

So, is this a PEBKAC? Am I using the nixos module wrong or should we have a setting for which order to mount things?

algernon commented 8 months ago

I think I ran into something very similar, although my setup is a little bit different: I have my entire /home on tmpfs... but it shouldn't really matter. In any case, what you want is neededForBoot = true for the /home mount. As far as I see, that should fix your issue.

If it doesn't, then if you follow what I did, and put /home on tmpfs, and create your user directory via boot.initrd.postMountCommands, that definitely works, I'm using that. =)

(But I'm also relatively new to NixOS, so your mileage may vary)

Erethon commented 8 months ago

In any case, what you want is neededForBoot = true for the /home mount. As far as I see, that should fix your issue.

You're correct, that fixed the issue, thank you!

It's documented too, but for some reason I failed to properly parse it. The wording made me think it would have the opposite effect of what it did.