nix-community / impermanence

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

Add environment.impermanence #2

Closed etu closed 4 years ago

etu commented 4 years ago

This new module have the same usage as the NixOS module but it touches files and create directories where needed and then use bind mounts instead of symlinks to access the files.

This is a first step to matching the ideas in #1, but I would like to have the users bit as well further on.

But I still this stand alone can be worth it to begin with if you want to have bind mounts instead.

My local usage:

{
  environment.impermanence."/persistent" = {
    directories = [
      "/etc/nixos"
      "/etc/NetworkManager/system-connections"
    ];
    files = [
      "/etc/machine-id"
      "/etc/ssh/ssh_host_rsa_key"
      "/etc/ssh/ssh_host_rsa_key.pub"
      "/etc/ssh/ssh_host_ed25519_key"
      "/etc/ssh/ssh_host_ed25519_key.pub"
    ];
  };
}

This gives me the following in /etc/fstab:

/persistent/etc/NetworkManager/system-connections /etc/NetworkManager/system-connections auto bind 0 2
/persistent/etc/machine-id /etc/machine-id auto bind 0 2
/persistent/etc/nixos /etc/nixos auto bind 0 2
/persistent/etc/ssh/ssh_host_ed25519_key /etc/ssh/ssh_host_ed25519_key auto bind 0 2
/persistent/etc/ssh/ssh_host_ed25519_key.pub /etc/ssh/ssh_host_ed25519_key.pub auto bind 0 2
/persistent/etc/ssh/ssh_host_rsa_key /etc/ssh/ssh_host_rsa_key auto bind 0 2
/persistent/etc/ssh/ssh_host_rsa_key.pub /etc/ssh/ssh_host_rsa_key.pub auto bind 0 2

And the following in /var/run/current-system/activate

#### Activation script snippet createFilesAndDirsIn--persistent:
_localstatus=0
mkdir -p $(dirname "/etc/machine-id") $(dirname "/persistent/etc/machine-id") &&
  touch "/etc/machine-id" "/persistent/etc/machine-id"
mkdir -p $(dirname "/etc/ssh/ssh_host_rsa_key") $(dirname "/persistent/etc/ssh/ssh_host_rsa_key") &&
  touch "/etc/ssh/ssh_host_rsa_key" "/persistent/etc/ssh/ssh_host_rsa_key"
mkdir -p $(dirname "/etc/ssh/ssh_host_rsa_key.pub") $(dirname "/persistent/etc/ssh/ssh_host_rsa_key.pub") &&
  touch "/etc/ssh/ssh_host_rsa_key.pub" "/persistent/etc/ssh/ssh_host_rsa_key.pub"
mkdir -p $(dirname "/etc/ssh/ssh_host_ed25519_key") $(dirname "/persistent/etc/ssh/ssh_host_ed25519_key") &&
  touch "/etc/ssh/ssh_host_ed25519_key" "/persistent/etc/ssh/ssh_host_ed25519_key"
mkdir -p $(dirname "/etc/ssh/ssh_host_ed25519_key.pub") $(dirname "/persistent/etc/ssh/ssh_host_ed25519_key.pub") &&
  touch "/etc/ssh/ssh_host_ed25519_key.pub" "/persistent/etc/ssh/ssh_host_ed25519_key.pub"
mkdir -p "/etc/nixos" "/persistent/etc/nixos"
mkdir -p "/etc/NetworkManager/system-connections" "/persistent/etc/NetworkManager/system-connections"