nixfiles/nixos/modules/tmproot.nix

334 lines
10 KiB
Nix
Raw Normal View History

2022-03-26 14:20:30 +00:00
{ lib, pkgs, options, config, ... }:
2022-02-13 13:10:21 +00:00
let
2022-03-26 14:20:30 +00:00
inherit (lib)
optionalString concatStringsSep concatMap concatMapStringsSep mkIf mkDefault mkMerge mkForce mkVMOverride
mkAliasDefinitions;
inherit (lib.my) mkOpt' mkBoolOpt' mkVMOverride';
2022-02-13 13:10:21 +00:00
cfg = config.my.tmproot;
2022-03-26 14:20:30 +00:00
enablePersistence = cfg.persistence.dir != null;
2022-02-13 13:10:21 +00:00
showUnsaved =
''
#!${pkgs.python310}/bin/python
import stat
import sys
import os
ignored = [
${concatStringsSep ",\n " (map (p: "'${p}'") cfg.unsaved.ignore)}
]
base = '/'
base_dev = os.stat(base).st_dev
def recurse(p, link=None):
try:
for ignore in ignored:
if p.startswith(ignore):
2022-02-09 20:41:51 +00:00
return
2022-02-13 13:10:21 +00:00
st = os.lstat(p)
if st.st_dev != base_dev:
return
2022-02-09 20:41:51 +00:00
2022-02-13 13:10:21 +00:00
if stat.S_ISLNK(st.st_mode):
target = os.path.realpath(p, strict=False)
if os.access(target, os.F_OK):
recurse(target, link=p)
return
if ${if enablePersistence then "True" else "False"} and target.startswith('${cfg.persistence.dir}'):
# A symlink whose target cannot be accessed but starts with /persist... almost certaily re-generated on
# activation
return
2022-02-13 13:10:21 +00:00
elif stat.S_ISDIR(st.st_mode):
for e in os.listdir(p):
recurse(os.path.join(p, e))
return
print(link or p)
except PermissionError as ex:
print(f'{p}: {ex.strerror}', file=sys.stderr)
recurse(base)
'';
rootDef = {
device = "yeet";
fsType = "tmpfs";
2022-02-19 22:55:53 +00:00
# The default mode for tmpfs is 777
options = [ "size=${cfg.size}" "mode=755" ];
2022-02-13 13:10:21 +00:00
};
2022-06-12 02:40:57 +01:00
persistSimpleSvc = n: mkIf config.services."${n}".enable {
my.tmproot.persistence.config.directories = [
{
directory = "/var/lib/${n}";
inherit (config.services."${n}") user group;
}
];
};
2022-02-13 13:10:21 +00:00
in
{
options = with lib.types; {
my.tmproot = {
2022-02-13 17:44:14 +00:00
enable = mkBoolOpt' true "Whether to enable tmproot.";
size = mkOpt' str "2G" "Size of tmpfs root";
2022-03-26 14:20:30 +00:00
persistence = {
dir = mkOpt' (nullOr str) "/persist" "Path where persisted files are stored.";
config = mkOpt' options.environment.persistence.type.nestedTypes.elemType { } "Persistence configuration";
};
2022-02-13 13:10:21 +00:00
unsaved = {
2022-02-13 17:44:14 +00:00
showMotd = mkBoolOpt' true "Whether to show unsaved files with `dynamic-motd`.";
ignore = mkOpt' (listOf str) [ ] "Path prefixes to ignore if unsaved.";
};
2022-02-11 13:26:33 +00:00
};
2022-02-13 13:10:21 +00:00
};
config = mkIf cfg.enable (mkMerge [
{
assertions = [
{
# I mean you probably _could_, but if you're doing tmproot... come on
assertion = !config.users.mutableUsers;
message = "users.mutableUsers is incompatible with tmproot";
}
];
2022-03-26 14:20:30 +00:00
my.tmproot = {
unsaved.ignore = [
"/tmp"
2022-02-13 13:10:21 +00:00
2022-03-26 14:20:30 +00:00
# setup-etc.pl will create this for us
"/etc/NIXOS"
2022-02-13 13:10:21 +00:00
2022-03-26 14:20:30 +00:00
# Once mutableUsers is disabled, we should be all clear here
"/etc/passwd"
"/etc/group"
"/etc/shadow"
"/etc/subuid"
"/etc/subgid"
2022-02-13 13:10:21 +00:00
2022-03-26 14:20:30 +00:00
# Lock file for /etc/{passwd,shadow}
"/etc/.pwd.lock"
2022-02-13 13:10:21 +00:00
2022-03-26 14:20:30 +00:00
# systemd last updated? I presume they'll get updated on boot...
"/etc/.updated"
"/var/.updated"
2022-02-13 13:10:21 +00:00
2022-03-26 14:20:30 +00:00
# Specifies obsolete files that should be deleted on activation - we'll never have those!
"/etc/.clean"
2022-02-19 22:55:53 +00:00
2022-03-26 14:20:30 +00:00
# These are set in environment.etc by the sshd module, but because their mode needs to be changed,
# setup-etc will copy them instead of symlinking
"/etc/ssh/authorized_keys.d"
2022-05-28 13:57:01 +01:00
# Auto-generated (on activation?)
"/root/.nix-channels"
"/root/.nix-defexpr"
2022-05-31 21:25:51 +01:00
"/var/lib/logrotate.status"
2022-03-26 14:20:30 +00:00
];
persistence.config = {
# In impermanence the key in `environment.persistence.*` (aka name passed the attrsOf submodule) sets the
# default value, so we need to override it when we mkAliasDefinitions
_module.args.name = mkForce cfg.persistence.dir;
hideMounts = mkDefault true;
directories = [
"/var/log"
# In theory we'd include only the files needed individually (i.e. the {U,G}ID map files that track deleted
# users and groups), but `update-users-groups.pl` actually deletes the original files for "atomic update".
# Also the script runs before impermanence does.
"/var/lib/nixos"
"/var/lib/systemd"
2022-06-17 23:15:39 +01:00
{ directory = "/root/.cache/nix"; mode = "0700"; }
2022-03-26 14:20:30 +00:00
];
files = [
"/etc/machine-id"
];
};
};
2022-02-13 13:10:21 +00:00
environment.systemPackages = [
(pkgs.writeScriptBin "tmproot-unsaved" showUnsaved)
];
my.dynamic-motd.script = mkIf cfg.unsaved.showMotd
''
tmprootUnsaved() {
local count="$(tmproot-unsaved | wc -l)"
[ $count -eq 0 ] && return
echo
echo -e "\t\e[31;1;4mWarning:\e[0m $count file(s) on / will be lost on shutdown!"
echo -e '\tTo see them, run `tmproot-unsaved` as root.'
${optionalString enablePersistence ''
2022-03-26 14:20:30 +00:00
echo -e '\tAdd these files to `my.tmproot.persistence.config` to keep them!'
''}
echo -e "\tIf they don't need to be kept, add them to \`my.tmproot.unsaved.ignore\`."
2022-02-13 13:10:21 +00:00
echo
}
2022-02-13 04:01:22 +00:00
2022-02-13 13:10:21 +00:00
tmprootUnsaved
'';
2022-02-13 04:01:22 +00:00
2022-02-13 13:10:21 +00:00
fileSystems."/" = rootDef;
}
2022-02-13 13:10:21 +00:00
(mkIf config.networking.resolvconf.enable {
my.tmproot.unsaved.ignore = [ "/etc/resolv.conf" ];
})
(mkIf config.security.doas.enable {
my.tmproot.unsaved.ignore = [ "/etc/doas.conf" ];
})
2022-05-31 21:25:51 +01:00
(mkIf config.services.resolved.enable {
my.tmproot.unsaved.ignore = [ "/etc/resolv.conf" ];
})
2022-02-17 15:47:24 +00:00
(mkIf config.my.build.isDevVM {
2022-02-13 13:10:21 +00:00
my.tmproot.unsaved.ignore = [ "/nix" ];
fileSystems = mkVMOverride {
"/" = mkVMOverride' rootDef;
};
})
(mkIf enablePersistence (mkMerge [
{
assertions = [
{
2022-03-26 14:20:30 +00:00
assertion = (config.fileSystems ? "${cfg.persistence.dir}") || config.boot.isContainer;
message = "The 'fileSystems' option does not specify your persistence file system (${cfg.persistence.dir}).";
}
];
# Catch non-existent source directories that are needed for boot (see `pathsNeededForBoot` in
# nixos/lib/util.nix). We do this by monkey-patching the `waitDevice` function that would otherwise hang.
boot.initrd.postDeviceCommands =
''
ensurePersistSource() {
[ -e "/mnt-root$1" ] && return
echo "Persistent source directory $1 does not exist, creating..."
install -dm "$2" "/mnt-root$1" || fail
}
_waitDevice() {
local device="$1"
${concatMapStringsSep " || \\\n " (d:
let
sourceDir = "${d.persistentStoragePath}${d.directory}";
in
''([ "$device" = "/mnt-root${sourceDir}" ] && ensurePersistSource "${sourceDir}" "${d.mode}")'')
2022-03-26 14:20:30 +00:00
cfg.persistence.config.directories}
waitDevice "$@"
}
type waitDevice > /dev/null || (echo "waitDevice is missing!"; fail)
alias waitDevice=_waitDevice
'';
2022-03-26 14:20:30 +00:00
environment.persistence."${cfg.persistence.dir}" = mkAliasDefinitions options.my.tmproot.persistence.config;
virtualisation = {
diskImage = "./.vms/${config.system.name}-persist.qcow2";
};
}
(mkIf config.services.openssh.enable {
2022-03-26 14:20:30 +00:00
my.tmproot.persistence.config.files =
concatMap (k: [ k.path "${k.path}.pub" ]) config.services.openssh.hostKeys;
})
2022-06-06 00:18:24 +01:00
(mkIf (config.security.acme.certs != { }) {
my.tmproot.persistence.config.directories = [
{
directory = "/var/lib/acme";
mode = "0750";
user = "acme";
group = "acme";
}
];
})
2022-06-06 17:52:36 +01:00
(mkIf config.services.postgresql.enable {
my.tmproot.persistence.config.directories = [
{
directory = "/var/lib/postgresql";
mode = "0750";
user = "postgres";
group = "postgres";
}
];
})
2022-06-10 23:25:55 +01:00
(mkIf config.services.matrix-synapse.enable {
my.tmproot.persistence.config.directories = [
{
directory = config.services.matrix-synapse.dataDir;
user = "matrix-synapse";
group = "matrix-synapse";
}
];
})
2022-06-12 14:56:44 +01:00
(mkIf config.services.jellyfin.enable {
my.tmproot.persistence.config.directories = [
{
directory = "/var/lib/jellyfin";
inherit (config.services.jellyfin) user group;
}
{
directory = "/var/cache/jellyfin";
inherit (config.services.jellyfin) user group;
}
];
})
2022-06-12 17:27:11 +01:00
(mkIf config.services.netdata.enable {
my.tmproot.persistence.config.directories = [
{
directory = "/var/lib/netdata";
inherit (config.services.netdata) user group;
}
{
directory = "/var/cache/netdata";
inherit (config.services.netdata) user group;
}
];
})
2022-07-16 21:01:18 +01:00
(mkIf config.services.hercules-ci-agent.enable {
my.tmproot.persistence.config.directories = [
{
directory = config.services.hercules-ci-agent.settings.baseDirectory;
mode = "0750";
user = "hercules-ci-agent";
group = "hercules-ci-agent";
}
];
})
2022-06-12 02:40:57 +01:00
(persistSimpleSvc "transmission")
(persistSimpleSvc "jackett")
(persistSimpleSvc "radarr")
(persistSimpleSvc "sonarr")
2022-07-16 15:01:15 +01:00
(mkIf config.services.minio.enable {
my.tmproot.persistence.config.directories = [
{
directory = config.services.minio.configDir;
user = "minio";
group = "minio";
}
];
})
2022-02-17 15:47:24 +00:00
(mkIf config.my.build.isDevVM {
fileSystems = mkVMOverride {
# Hijack the "root" device for persistence in the VM
2022-03-26 14:20:30 +00:00
"${cfg.persistence.dir}" = {
device = config.virtualisation.bootDevice;
neededForBoot = true;
};
};
})
]))
2022-02-13 13:10:21 +00:00
]);
meta.buildDocsInSandbox = false;
2022-02-13 13:10:21 +00:00
}