feat: fix cache cleanup and allow to set bind method for home persist module
This commit is contained in:
parent
942a8686b5
commit
0225ae25aa
@ -8,12 +8,20 @@
|
|||||||
let
|
let
|
||||||
inherit (lib)
|
inherit (lib)
|
||||||
escapeShellArg
|
escapeShellArg
|
||||||
|
isAttrs
|
||||||
mkEnableOption
|
mkEnableOption
|
||||||
mkIf
|
mkIf
|
||||||
mkOption
|
mkOption
|
||||||
recursiveUpdate
|
recursiveUpdate
|
||||||
;
|
;
|
||||||
inherit (lib.types) listOf path str;
|
inherit (lib.types)
|
||||||
|
either
|
||||||
|
enum
|
||||||
|
listOf
|
||||||
|
path
|
||||||
|
str
|
||||||
|
submodule
|
||||||
|
;
|
||||||
inherit (builtins) concatMap;
|
inherit (builtins) concatMap;
|
||||||
cfg = config.persist;
|
cfg = config.persist;
|
||||||
username = config.home.username;
|
username = config.home.username;
|
||||||
@ -25,14 +33,47 @@ in
|
|||||||
|
|
||||||
options =
|
options =
|
||||||
let
|
let
|
||||||
|
directoryEntryType = submodule {
|
||||||
|
options = {
|
||||||
|
directory = mkOption {
|
||||||
|
type = str;
|
||||||
|
description = "The directory path to be linked.";
|
||||||
|
};
|
||||||
|
method = mkOption {
|
||||||
|
type = enum [
|
||||||
|
"bindfs"
|
||||||
|
"symlink"
|
||||||
|
];
|
||||||
|
default = config.defaultDirectoryMethod;
|
||||||
|
description = ''
|
||||||
|
The linking method to be used for this specific directory entry.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
common = {
|
common = {
|
||||||
directories = mkOption {
|
directories = mkOption {
|
||||||
type = listOf str;
|
type = listOf (either str directoryEntryType);
|
||||||
default = [ ];
|
default = [ ];
|
||||||
|
description = ''
|
||||||
|
List of directories to persist.
|
||||||
|
Each element can be a string (e.g., ".cache") or an attribute set
|
||||||
|
(e.g., { directory = ".local/share/Steam"; method = "symlink"; }).
|
||||||
|
'';
|
||||||
|
example = [
|
||||||
|
".config/foo"
|
||||||
|
{
|
||||||
|
directory = ".config/bar";
|
||||||
|
method = "symlink";
|
||||||
|
}
|
||||||
|
];
|
||||||
};
|
};
|
||||||
files = mkOption {
|
files = mkOption {
|
||||||
type = listOf str;
|
type = listOf str;
|
||||||
default = [ ];
|
default = [ ];
|
||||||
|
description = "List of files to persist.";
|
||||||
|
example = [ ".config/foo.conf" ];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
in
|
in
|
||||||
@ -43,12 +84,10 @@ in
|
|||||||
type = path;
|
type = path;
|
||||||
default = "/persist${config.home.homeDirectory}";
|
default = "/persist${config.home.homeDirectory}";
|
||||||
};
|
};
|
||||||
# Stuff that matters
|
|
||||||
# TODO backups
|
# TODO backups
|
||||||
state = recursiveUpdate {
|
state = recursiveUpdate {
|
||||||
# backup = {...};
|
# backup = {...};
|
||||||
} common;
|
} common;
|
||||||
# Stuff that's just there to speed up the system
|
|
||||||
cache = recursiveUpdate {
|
cache = recursiveUpdate {
|
||||||
clean = {
|
clean = {
|
||||||
enable = mkEnableOption "cleaning the cache files and directories";
|
enable = mkEnableOption "cleaning the cache files and directories";
|
||||||
@ -62,7 +101,6 @@ in
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
# TODO: filter persist paths like in nixos module
|
|
||||||
config =
|
config =
|
||||||
let
|
let
|
||||||
takeAll = what: concatMap (x: x.${what});
|
takeAll = what: concatMap (x: x.${what});
|
||||||
@ -72,6 +110,9 @@ in
|
|||||||
];
|
];
|
||||||
allFiles = takeAll "files" persists;
|
allFiles = takeAll "files" persists;
|
||||||
allDirs = takeAll "directories" persists;
|
allDirs = takeAll "directories" persists;
|
||||||
|
|
||||||
|
# Helper function to extract path strings from the mixed list
|
||||||
|
getPaths = map (x: if isAttrs x then x.directory else x);
|
||||||
in
|
in
|
||||||
mkIf cfg.enable {
|
mkIf cfg.enable {
|
||||||
home.persistence.${cfg.persistRoot} = {
|
home.persistence.${cfg.persistRoot} = {
|
||||||
@ -91,8 +132,8 @@ in
|
|||||||
"Videos"
|
"Videos"
|
||||||
".config/dconf"
|
".config/dconf"
|
||||||
".local/share/nix"
|
".local/share/nix"
|
||||||
|
".local/share/systemd"
|
||||||
".ssh"
|
".ssh"
|
||||||
# { directory = ".ssh"; mode = "0700"; }
|
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -102,14 +143,21 @@ in
|
|||||||
Description = "Cleaning up cache files and directories for user ${username}";
|
Description = "Cleaning up cache files and directories for user ${username}";
|
||||||
Wants = [ "modprobed-db.timer" ];
|
Wants = [ "modprobed-db.timer" ];
|
||||||
};
|
};
|
||||||
Service = {
|
Service =
|
||||||
|
let
|
||||||
|
# Extract only the path strings for the cleanup script
|
||||||
|
cacheDirPaths = getPaths cfg.cache.directories;
|
||||||
|
in
|
||||||
|
{
|
||||||
ExecStart = pkgs.writeShellScript "" ''
|
ExecStart = pkgs.writeShellScript "" ''
|
||||||
${builtins.concatStringsSep "\n" (
|
${builtins.concatStringsSep "\n" (
|
||||||
map (x: "rm ${escapeShellArg x}") (absoluteHomePath cfg.cache.files)
|
map (x: "${pkgs.coreutils}/bin/rm ${escapeShellArg x}") (absoluteHomePath cfg.cache.files)
|
||||||
)}
|
)}
|
||||||
|
|
||||||
${builtins.concatStringsSep "\n" (
|
${builtins.concatStringsSep "\n" (
|
||||||
map (x: "rm -rf ${escapeShellArg x}") (absoluteHomePath cfg.cache.directories)
|
map (x: "${pkgs.findutils}/bin/find ${escapeShellArg x} -mindepth 1 -delete") (
|
||||||
|
absoluteHomePath cacheDirPaths
|
||||||
|
)
|
||||||
)}
|
)}
|
||||||
'';
|
'';
|
||||||
Type = "simple";
|
Type = "simple";
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
config,
|
config,
|
||||||
lib,
|
lib,
|
||||||
|
pkgs,
|
||||||
inputs,
|
inputs,
|
||||||
...
|
...
|
||||||
}:
|
}:
|
||||||
@ -8,12 +9,10 @@ let
|
|||||||
inherit (lib)
|
inherit (lib)
|
||||||
escapeShellArg
|
escapeShellArg
|
||||||
hasPrefix
|
hasPrefix
|
||||||
hasSuffix
|
|
||||||
mkEnableOption
|
mkEnableOption
|
||||||
mkDefault
|
mkDefault
|
||||||
mkIf
|
mkIf
|
||||||
mkOption
|
mkOption
|
||||||
optionalString
|
|
||||||
recursiveUpdate
|
recursiveUpdate
|
||||||
unique
|
unique
|
||||||
;
|
;
|
||||||
@ -23,8 +22,7 @@ let
|
|||||||
|
|
||||||
btrfs = config.ataraxia.filesystems.btrfs.mountpoints;
|
btrfs = config.ataraxia.filesystems.btrfs.mountpoints;
|
||||||
zfs = config.ataraxia.filesystems.zfs.mountpoints;
|
zfs = config.ataraxia.filesystems.zfs.mountpoints;
|
||||||
mountpoints = map (x: "${x}${optionalString (!(hasSuffix "/" x)) "/"}") (unique (btrfs ++ zfs));
|
mountpoints = unique (btrfs ++ zfs);
|
||||||
|
|
||||||
subtractListsPrefix = a: filter (dir: !(any (pref: hasPrefix pref dir) a));
|
subtractListsPrefix = a: filter (dir: !(any (pref: hasPrefix pref dir) a));
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
@ -94,9 +92,13 @@ in
|
|||||||
systemd.services.persist-cache-cleanup = mkIf cfg.cache.clean.enable {
|
systemd.services.persist-cache-cleanup = mkIf cfg.cache.clean.enable {
|
||||||
description = "Cleaning up cache files and directories";
|
description = "Cleaning up cache files and directories";
|
||||||
script = ''
|
script = ''
|
||||||
${builtins.concatStringsSep "\n" (map (x: "rm ${escapeShellArg x}") cfg.cache.files)}
|
${builtins.concatStringsSep "\n" (
|
||||||
|
map (x: "${pkgs.coreutils}/bin/rm ${escapeShellArg x}") cfg.cache.files
|
||||||
|
)}
|
||||||
|
|
||||||
${builtins.concatStringsSep "\n" (map (x: "rm -rf ${escapeShellArg x}") cfg.cache.directories)}
|
${builtins.concatStringsSep "\n" (
|
||||||
|
map (x: "${pkgs.findutils}/bin/find ${escapeShellArg x} -mindepth 1 -delete") cfg.cache.directories
|
||||||
|
)}
|
||||||
'';
|
'';
|
||||||
startAt = cfg.cache.clean.dates;
|
startAt = cfg.cache.clean.dates;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user