Skip to content

Instantly share code, notes, and snippets.

@bzm3r
Created November 30, 2023 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bzm3r/410fc1bcd1f22426a266a7af2b61bdf3 to your computer and use it in GitHub Desktop.
Save bzm3r/410fc1bcd1f22426a266a7af2b61bdf3 to your computer and use it in GitHub Desktop.
How to not worry, and still be happy.
{
config,
lib,
pkgs,
_user,
...
}: let
inherit (builtins) replaceStrings;
inherit (lib.attrsets) mapAttrsToList;
inherit (lib.lists) flatten;
inherit (lib.modules) mkIf mkDefault mkDerivedConfig;
inherit (lib.options) mkOption;
inherit (lib.types) attrsOf lines nullOr path str submodule;
_tmpfileType = prefix:
attrsOf (submodule ({
config,
options,
name,
...
}: {
options = {
source = mkOption {type = path;};
target = mkOption {type = str;};
text = mkOption {
default = null;
type = nullOr lines;
};
};
config = {
source = mkIf (config.text != null) (mkDerivedConfig options.text (pkgs.writeText "xdg-${prefix}-${replaceStrings ["/"] ["-"] name}"));
target = mkDefault name;
};
}));
in {
options = {
home.file = mkOption {
default = {};
type = _tmpfileType "homeFile";
};
xdg = {
configFile = mkOption {
default = {};
type = _tmpfileType "configFile";
};
dataFile = mkOption {
default = {};
type = _tmpfileType "dataFile";
};
};
};
config.systemd.user.tmpfiles.users.${_user}.rules = let
_tmpStr = prefix: _: file: "L+ '${prefix}/${file.target}' - - - - ${file.source}";
in
flatten [
(mapAttrsToList (_tmpStr "%h") config.home.file)
(mapAttrsToList (_tmpStr "%h/.config") config.xdg.configFile)
(mapAttrsToList (_tmpStr "%h/.local/share") config.xdg.dataFile)
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment