2025-03-02 16:50:12 +03:00
|
|
|
{
|
2025-03-10 18:45:57 +03:00
|
|
|
config,
|
2025-03-02 16:50:12 +03:00
|
|
|
lib,
|
2025-06-07 17:20:39 +03:00
|
|
|
pkgs,
|
2025-06-07 17:47:20 +03:00
|
|
|
inputs,
|
2025-03-02 16:50:12 +03:00
|
|
|
...
|
|
|
|
}:
|
|
|
|
let
|
2025-03-10 20:25:56 +03:00
|
|
|
inherit (lib)
|
2025-06-07 17:20:39 +03:00
|
|
|
escapeShellArg
|
2025-03-10 20:25:56 +03:00
|
|
|
mkEnableOption
|
|
|
|
mkIf
|
|
|
|
mkOption
|
|
|
|
recursiveUpdate
|
|
|
|
;
|
2025-03-02 16:50:12 +03:00
|
|
|
inherit (lib.types) listOf path str;
|
2025-06-07 17:20:39 +03:00
|
|
|
inherit (builtins) concatMap;
|
2025-03-10 18:45:57 +03:00
|
|
|
cfg = config.persist;
|
2025-06-07 17:20:39 +03:00
|
|
|
username = config.home.username;
|
|
|
|
homeDir = config.home.homeDirectory;
|
|
|
|
absoluteHomePath = map (x: "${homeDir}/${x}");
|
2025-03-02 16:50:12 +03:00
|
|
|
in
|
|
|
|
{
|
2025-06-07 17:47:20 +03:00
|
|
|
imports = [ inputs.impermanence.homeManagerModules.impermanence ];
|
|
|
|
|
2025-03-02 16:50:12 +03:00
|
|
|
options =
|
|
|
|
let
|
|
|
|
common = {
|
|
|
|
directories = mkOption {
|
|
|
|
type = listOf str;
|
|
|
|
default = [ ];
|
|
|
|
};
|
|
|
|
files = mkOption {
|
|
|
|
type = listOf str;
|
|
|
|
default = [ ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
persist = {
|
|
|
|
enable = mkEnableOption "A tmpfs root with explicit opt-in state";
|
|
|
|
persistRoot = mkOption {
|
|
|
|
type = path;
|
2025-06-07 17:20:39 +03:00
|
|
|
default = "/persist${config.home.homeDirectory}";
|
2025-03-02 16:50:12 +03:00
|
|
|
};
|
|
|
|
# Stuff that matters
|
|
|
|
# TODO backups
|
2025-03-10 20:25:56 +03:00
|
|
|
state = recursiveUpdate {
|
2025-03-02 16:50:12 +03:00
|
|
|
# backup = {...};
|
2025-03-10 20:25:56 +03:00
|
|
|
} common;
|
2025-03-02 16:50:12 +03:00
|
|
|
# Stuff that's just there to speed up the system
|
2025-03-10 20:25:56 +03:00
|
|
|
cache = recursiveUpdate {
|
2025-03-02 16:50:12 +03:00
|
|
|
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";
|
|
|
|
};
|
|
|
|
};
|
2025-03-10 20:25:56 +03:00
|
|
|
} common;
|
2025-03-02 16:50:12 +03:00
|
|
|
};
|
|
|
|
};
|
2025-03-10 18:45:57 +03:00
|
|
|
|
2025-06-07 17:20:39 +03:00
|
|
|
# TODO: filter persist paths like in nixos module
|
|
|
|
config =
|
|
|
|
let
|
|
|
|
takeAll = what: concatMap (x: x.${what});
|
|
|
|
persists = with cfg; [
|
|
|
|
state
|
|
|
|
cache
|
|
|
|
];
|
|
|
|
allFiles = takeAll "files" persists;
|
|
|
|
allDirs = takeAll "directories" persists;
|
|
|
|
in
|
|
|
|
mkIf cfg.enable {
|
|
|
|
home.persistence.${cfg.persistRoot} = {
|
|
|
|
allowOther = true;
|
|
|
|
directories = allDirs;
|
|
|
|
files = allFiles;
|
|
|
|
};
|
|
|
|
|
|
|
|
# Persist by default
|
|
|
|
persist.cache.directories = [ ".cache" ];
|
|
|
|
persist.state = {
|
|
|
|
directories = [
|
|
|
|
"Downloads"
|
|
|
|
"Documents"
|
|
|
|
"Music"
|
|
|
|
"Pictures"
|
|
|
|
"Videos"
|
|
|
|
".config/dconf"
|
|
|
|
".local/share/nix"
|
|
|
|
".ssh"
|
|
|
|
# { directory = ".ssh"; mode = "0700"; }
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.user = mkIf cfg.cache.clean.enable {
|
|
|
|
services."persist-cache-cleanup-${username}" = {
|
|
|
|
Unit = {
|
|
|
|
Description = "Cleaning up cache files and directories for user ${username}";
|
|
|
|
Wants = [ "modprobed-db.timer" ];
|
|
|
|
};
|
|
|
|
Service = {
|
|
|
|
ExecStart = pkgs.writeShellScript "" ''
|
|
|
|
${builtins.concatStringsSep "\n" (
|
|
|
|
map (x: "rm ${escapeShellArg x}") (absoluteHomePath cfg.cache.files)
|
|
|
|
)}
|
|
|
|
|
|
|
|
${builtins.concatStringsSep "\n" (
|
|
|
|
map (x: "rm -rf ${escapeShellArg x}") (absoluteHomePath cfg.cache.directories)
|
|
|
|
)}
|
|
|
|
'';
|
|
|
|
Type = "simple";
|
|
|
|
};
|
|
|
|
Install.WantedBy = [ "default.target" ];
|
|
|
|
};
|
|
|
|
timers."persist-cache-cleanup-${username}" = {
|
|
|
|
Unit = {
|
|
|
|
Description = "Run persist-cache-cleanup-${username} service by set schedule";
|
|
|
|
PartOf = [ "persist-cache-cleanup-${username}.service" ];
|
|
|
|
};
|
|
|
|
Timer = {
|
|
|
|
Persistent = true;
|
|
|
|
OnCalendar = cfg.cache.clean.dates;
|
|
|
|
};
|
|
|
|
Install.WantedBy = [ "timers.target" ];
|
|
|
|
};
|
|
|
|
};
|
2025-03-10 18:45:57 +03:00
|
|
|
};
|
2025-03-02 16:50:12 +03:00
|
|
|
}
|