nixos-config/modules/persist.nix

112 lines
2.8 KiB
Nix
Raw Permalink Normal View History

2024-02-08 23:21:10 +03:00
{ config, lib, inputs, ... }:
2022-12-14 23:46:25 +03:00
let
cfg = config.persist;
takeAll = what: concatMap (x: x.${what});
2024-06-19 15:49:07 +03:00
persists = with cfg; [ state cache ];
2022-12-14 23:46:25 +03:00
2023-01-26 00:41:28 +03:00
absoluteHomePath = map (x: "${cfg.homeDir}/${x}");
2022-12-14 23:46:25 +03:00
allFiles = takeAll "files" persists;
allHomeFiles = takeAll "homeFiles" persists;
allDirectories = takeAll "directories" persists;
allHomeDirectories = takeAll "homeDirectories" persists;
inherit (builtins) concatMap;
inherit (lib) mkIf;
2023-11-22 06:21:44 +03:00
homeDirectory = config.home-manager.users.${config.mainuser}.home.homeDirectory or "/home/${config.mainuser}";
2022-12-14 23:46:25 +03:00
in {
options = let
inherit (lib) mkOption mkEnableOption;
2024-02-08 23:21:10 +03:00
inherit (lib.types) listOf path str;
2023-01-26 00:41:28 +03:00
2022-12-14 23:46:25 +03:00
common = {
directories = mkOption {
2023-01-26 00:41:28 +03:00
type = listOf str;
2022-12-14 23:46:25 +03:00
default = [ ];
};
files = mkOption {
type = listOf str;
default = [ ];
};
2023-01-26 00:27:05 +03:00
homeFiles = mkOption {
2022-12-14 23:46:25 +03:00
type = listOf str;
default = [ ];
};
2023-01-26 00:27:05 +03:00
homeDirectories = mkOption {
2023-01-26 00:41:28 +03:00
type = listOf str;
2022-12-14 23:46:25 +03:00
default = [ ];
};
};
in {
persist = {
enable = mkEnableOption "a tmpfs root with explicit opt-in state";
persistRoot = mkOption {
type = path;
2023-01-26 00:27:05 +03:00
default = "/persist";
2022-12-14 23:46:25 +03:00
};
homeDir = mkOption {
type = path;
default = homeDirectory;
};
# Stuff that matters
# TODO backups of this stuff
state = {
# backup = {...};
} // common;
# Stuff that's just there to speed up the system
# It's cleaned up regularly, to solve the cache invalidation problem once and for all
cache = {
clean = {
enable = mkEnableOption "cleaning the cache files and directories";
dates = mkOption {
type = str;
default = "weekly";
description =
"A systemd.time calendar description of when to clean the cache files";
};
};
} // common;
};
};
imports = [ inputs.impermanence.nixosModules.impermanence ];
config = mkIf cfg.enable {
environment.persistence.${cfg.persistRoot} = {
2023-01-26 00:41:28 +03:00
hideMounts = true;
2022-12-14 23:46:25 +03:00
directories = allDirectories;
files = allFiles;
2023-01-26 00:41:28 +03:00
users.${config.mainuser} = {
2024-01-13 02:33:55 +03:00
home = "/home/${config.mainuser}";
2022-12-14 23:46:25 +03:00
directories = allHomeDirectories;
files = allHomeFiles;
};
};
systemd.services.persist-cache-cleanup = lib.mkIf cfg.cache.clean.enable {
description = "Cleaning up cache files and directories";
script = ''
${builtins.concatStringsSep "\n" (map (x: "rm ${lib.escapeShellArg x}")
(cfg.cache.files
2023-01-26 00:41:28 +03:00
++ absoluteHomePath cfg.cache.homeFiles))}
2022-12-14 23:46:25 +03:00
${builtins.concatStringsSep "\n" (map (x: "rm -rf ${lib.escapeShellArg x}")
2023-01-26 00:41:28 +03:00
(cfg.cache.directories ++ absoluteHomePath cfg.cache.homeDirectories))}
2022-12-14 23:46:25 +03:00
'';
startAt = cfg.cache.clean.dates;
};
};
}