Impermanence: Difference between revisions

From NixOS Wiki
imported>Klaymore
Started page, wrote a ton
 
imported>Klaymore
Home Manager module uses Fuse mounts not bind mounts. Added more Plasma files to persist
Line 1: Line 1:
Impermanence in NixOS is where your root directory gets wiped every reboot (such as by mounting a tmpfs to /). Such a setup is possible because NixOS only needs <code>/boot</code> and <code>/nix</code> in order to boot, all other system files are simply links to files in <code>/nix</code>. <code>/boot</code> and <code>/nix</code> still need to be stored on a hard drive or SSD.
Impermanence in NixOS is where your root directory gets wiped every reboot (such as by mounting a tmpfs to /). Such a setup is possible because NixOS only needs <code>/boot</code> and <code>/nix</code> in order to boot, all other system files are simply links to files in <code>/nix</code>. <code>/boot</code> and <code>/nix</code> still need to be stored on a hard drive or SSD.


Example hardwareconfiguration.nix settings:
Example <code>hardwareconfiguration.nix</code> settings:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
   fileSystems."/" =
   fileSystems."/" =
Line 60: Line 60:
You can just make a home partition on a drive and mount it as normal, so everything in <code>/home</code> or <code>/home/username</code> will be persisted. If you want your home to be impermanent as well, then mount it on tmpfs the same way as root.
You can just make a home partition on a drive and mount it as normal, so everything in <code>/home</code> or <code>/home/username</code> will be persisted. If you want your home to be impermanent as well, then mount it on tmpfs the same way as root.


For persisting files in your home, you could simply use [https://github.com/nix-community/home-manager Home Manager] like normal. However, then files are stored read-only in the Nix store. In order to persist files while still being able to edit them like normal, you can use the [https://github.com/nix-community/impermanence Home Manager Impermanence module]. It will bind mount and link files as necessary.
For persisting files in your home, you could simply use [https://github.com/nix-community/home-manager Home Manager] like normal. However, then files are stored read-only in the Nix store. In order to persist files while still being writable, you can use the [https://github.com/nix-community/impermanence Home Manager Impermanence module]. It will fuse mount and link files as necessary.




Line 118: Line 118:
       allowOther = true;
       allowOther = true;
       directories = [
       directories = [
         ".config/gtk-3.0"  # bind mounted to /home/$USERNAME/.config/gtk-3.0
         ".config/gtk-3.0"  # fuse mounted to /home/$USERNAME/.config/gtk-3.0
         ".config/gtk-4.0"
         ".config/gtk-4.0"
         ".config/KDE"
         ".config/KDE"
Line 125: Line 125:
         ".config/xsettingsd"
         ".config/xsettingsd"
         ".kde"
         ".kde"
        ".local/share/baloo"
        ".local/share/dolphin"
        ".local/share/kactivitymanagerd"
        ".local/share/kate"
        ".local/share/klipper"
        ".local/share/konsole"
        ".local/share/kscreen"
        ".local/share/kwalletd"
        ".local/share/kxmlgui5"
        ".local/share/RecentDocuments"
        ".local/share/sddm"
       ];
       ];
       files = [
       files = [
Line 180: Line 192:
         ".config/user-dirs.dirs"
         ".config/user-dirs.dirs"
         ".config/user-dirs.locale"
         ".config/user-dirs.locale"
        ".local/share/krunnerstaterc"
        ".local/share/user-places.xbel"
        ".local/share/user-places.xbel.bak"
        ".local/share/user-places.xbel.tbcache"
       ];
       ];
     };
     };

Revision as of 02:53, 13 January 2022

Impermanence in NixOS is where your root directory gets wiped every reboot (such as by mounting a tmpfs to /). Such a setup is possible because NixOS only needs /boot and /nix in order to boot, all other system files are simply links to files in /nix. /boot and /nix still need to be stored on a hard drive or SSD.

Example hardwareconfiguration.nix settings:

  fileSystems."/" =
    { device = "none";
      fsType = "tmpfs";
      options = [ "size=3G" "mode=755" ]; # mode=755 so only root can write to those files
    };
  fileSystems."/home/username" =
    { device = "none";
      fsType = "tmpfs";  # Can be stored on normal drive or on tmpfs as well
      options = [ "size=4G" "mode=777" ]; 
    };
  fileSystems."/nix" =  # can be LUKS encrypted
    { device = "/dev/disk/by-uuid/UUID";
      fsType = "ext4";
    };
  fileSystems."/boot" =
    { device = "/dev/disk/by-uuid/UUID";
      fsType = "vfat";
    };
  # Can mount any other partitions as well


Persisting

Some files and folders should be persisted between reboots though (such as /etc/nixos/). This can be accomplished through bind mounts or by using the NixOS Impermanence module, which will set up bind mounts and links as needed.

Impermanence module:

{ config, pkgs, ... }:

let
  impermanence = builtins.fetchTarball "https://github.com/nix-community/impermanence/archive/master.tar.gz";
in
{
  imports = [ "${impermanence}/nixos.nix" ];

  # this folder is where the files will be stored (don't put it in tmpfs)
  environment.persistence."/nix/persist/system" = { 
    directories = [
      "/etc/nixos"    # bind mounted from /nix/persist/system/etc/nixos to /etc/nixos
      "/etc/NetworkManager"
      "/var/log"
      "/var/lib"
    ];
    files = [
#      "/etc/machine-id"
      "/etc/nix/id_rsa"
    ];
  };

}


Home Managing

You can just make a home partition on a drive and mount it as normal, so everything in /home or /home/username will be persisted. If you want your home to be impermanent as well, then mount it on tmpfs the same way as root.

For persisting files in your home, you could simply use Home Manager like normal. However, then files are stored read-only in the Nix store. In order to persist files while still being writable, you can use the Home Manager Impermanence module. It will fuse mount and link files as necessary.


Note: /home/user should be on a separate tmpfs, otherwise you'll get the error fuse: mountpoint not empty.


Example:

{ config, pkgs, ... }:
let
  home-manager = builtins.fetchTarball "https://github.com/nix-community/home-manager/archive/release-21.11.tar.gz";
  impermanence = builtins.fetchTarball "https://github.com/nix-community/impermanence/archive/master.tar.gz";
in
{
  imports = [
    (import "${home-manager}/nixos")
  ];

  programs.fuse.userAllowOther = true;

  # Home Manager config goes in here
  home-manager.users.$USERNAME = {
    home.homeDirectory = "/home/$USERNAME";
    imports = [ "${impermanence}/home-manager.nix" ];

    programs = {
      home-manager.enable = true;
      git = {   # can use home-manager normally as well as with persistence
        enable = true;
        userName  = "Example";
        userEmail = "Example@example.com";
      };
    };

    home.persistence."/nix/dotfiles" = {
      removePrefixDirectory = true;   # for GNU Stow styled dotfile folders
      allowOther = true;
      directories = [
        "Atom/.atom/atom-discord"   
        "Atom/.atom/packages"
        "Firefox/.mozilla"   # bind mounted from /nix/dotfiles to /home/$USERNAME/.mozilla
        "Clementine/.config/Clementine"
      ];
      files = [
        "Atom/.atom/config.cson"
        "Atom/.atom/github.cson"
      ];
    };


    # fricking KDE Plasma has a bazillion files and each needs to be linked individually
    # because they're all just shoved into ~/.config and not into a single folder.
    # It's separate from the other dotfiles so I can write ".config"
    # instead of "Plasma/.config"
    home.persistence."/nix/dotfiles/Plasma" = {
      removePrefixDirectory = false;
      allowOther = true;
      directories = [
        ".config/gtk-3.0"   # fuse mounted to /home/$USERNAME/.config/gtk-3.0
        ".config/gtk-4.0"
        ".config/KDE"
        ".config/kde.org"
        ".config/plasma-workspace"
        ".config/xsettingsd"
        ".kde"

        ".local/share/baloo"
        ".local/share/dolphin"
        ".local/share/kactivitymanagerd"
        ".local/share/kate"
        ".local/share/klipper"
        ".local/share/konsole"
        ".local/share/kscreen"
        ".local/share/kwalletd"
        ".local/share/kxmlgui5"
        ".local/share/RecentDocuments"
        ".local/share/sddm"
      ];
      files = [
        ".config/akregatorrc"
        ".config/baloofileinformationrc"
        ".config/baloofilerc"
        ".config/bluedevilglobalrc"
        ".config/device_automounter_kcmrc"
        ".config/dolphinrc"
        ".config/filetypesrc"
        ".config/gtkrc"
        ".config/gtkrc-2.0"
        ".config/gwenviewrc"
        ".config/kactivitymanagerd-pluginsrc"
        ".config/kactivitymanagerd-statsrc"
        ".config/kactivitymanagerd-switcher"
        ".config/kactivitymanagerdrc"
        ".config/katemetainfos"
        ".config/katerc"
        ".config/kateschemarc"
        ".config/katevirc"
        ".config/kcmfonts"
        ".config/kcminputrc"
        ".config/kconf_updaterc"
        ".config/kded5rc"
        ".config/kdeglobals"
        ".config/kgammarc"
        ".config/kglobalshortcutsrc"
        ".config/khotkeysrc"
        ".config/kmixrc"
        ".config/konsolerc"
        ".config/kscreenlockerrc"
        ".config/ksmserverrc"
        ".config/ksplashrc"
        ".config/ktimezonedrc"
        ".config/kwinrc"
        ".config/kwinrulesrc"
        ".config/kxkbrc"
        ".config/mimeapps.list"
        ".config/partitionmanagerrc"
        ".config/plasma-localerc"
        ".config/plasma-nm"
        ".config/plasma-org.kde.plasma.desktop-appletsrc"
        ".config/plasmanotifyrc"
        ".config/plasmarc"
        ".config/plasmashellrc"
        ".config/PlasmaUserFeedback"
        ".config/plasmawindowed-appletsrc"
        ".config/plasmawindowedrc"
        ".config/powermanagementprofilesrc"
        ".config/spectaclerc"
        ".config/startkderc"
        ".config/systemsettingsrc"
        ".config/Trolltech.conf"
        ".config/user-dirs.dirs"
        ".config/user-dirs.locale"

        ".local/share/krunnerstaterc"
        ".local/share/user-places.xbel"
        ".local/share/user-places.xbel.bak"
        ".local/share/user-places.xbel.tbcache"
      ];
    };

    home.stateVersion = "21.11";
  };
}


See Also

https://elis.nu/blog/2020/06/nixos-tmpfs-as-home/ - Examples of using the NixOS modules

https://grahamc.com/blog/erase-your-darlings - Explaining why you might want to do this. Uses ZFS snapshots instead of tmpfs.