nixos-config/nixos/modules/system/permown.nix

100 lines
2.7 KiB
Nix
Raw Normal View History

2019-10-24 02:20:38 +02:00
{ config, pkgs, lib, ... }:
2020-09-08 21:45:42 +02:00
2019-10-24 02:20:38 +02:00
with lib;
2020-09-08 21:45:42 +02:00
let
cfg = config.system.permown;
nameGenerator = path: "permown.${replaceStrings [ "/" ] [ "_" ] path}";
2021-11-01 09:20:42 +01:00
in
{
2019-10-24 02:20:38 +02:00
options.system.permown = mkOption {
2019-12-20 05:54:26 +01:00
default = { };
type = with types;
attrsOf (submodule ({ config, ... }: {
options = {
directory-mode = mkOption {
default = "=rwx";
2020-09-08 21:45:42 +02:00
type = types.str;
2019-12-20 05:54:26 +01:00
};
file-mode = mkOption {
default = "=rw";
2020-09-08 21:45:42 +02:00
type = types.str;
2019-12-20 05:54:26 +01:00
};
group = mkOption {
apply = x: if x == null then "" else x;
default = null;
type = types.nullOr types.str;
};
owner = mkOption { type = types.str; };
path = mkOption {
default = config._module.args.name;
type = types.path;
};
umask = mkOption {
default = "0027";
type = types.str;
};
2020-09-08 21:45:42 +02:00
timer = mkOption {
default = "hourly";
type = types.str;
description =
"OnCalendar string on how frequent should this command run";
};
2019-10-24 02:20:38 +02:00
};
2019-12-20 05:54:26 +01:00
}));
2019-10-24 02:20:38 +02:00
};
2021-11-01 09:20:42 +01:00
config =
let plans = lib.attrValues cfg;
2020-09-08 21:45:42 +02:00
2021-11-01 09:20:42 +01:00
in mkIf (plans != [ ]) {
2019-10-24 02:20:38 +02:00
2021-11-01 09:20:42 +01:00
system.activationScripts.permown =
let
mkdir = { path, ... }: ''
${pkgs.coreutils}/bin/mkdir -p ${path}
'';
in
concatMapStrings mkdir plans;
2019-10-24 02:20:38 +02:00
2021-11-01 09:20:42 +01:00
systemd.services = listToAttrs (flip map plans
({ path, directory-mode, file-mode, owner, group, umask, ... }: {
name = nameGenerator path;
value = {
environment = {
DIR_MODE = directory-mode;
FILE_MODE = file-mode;
OWNER_GROUP = "${owner}:${group}";
ROOT_PATH = path;
};
path = [ pkgs.coreutils pkgs.findutils pkgs.inotifyTools ];
serviceConfig = {
ExecStart = pkgs.writers.writeDash "permown" ''
set -efu
find "$ROOT_PATH" -exec chown -h "$OWNER_GROUP" {} +
find "$ROOT_PATH" -type d -exec chmod "$DIR_MODE" {} +
find "$ROOT_PATH" -type f -exec chmod "$FILE_MODE" {} +
'';
PrivateTmp = true;
2022-01-18 20:20:56 +01:00
#Restart = "always";
#RestartSec = 10;
2021-11-01 09:20:42 +01:00
UMask = umask;
};
wantedBy = [ "multi-user.target" ];
};
}));
systemd.timers = listToAttrs (flip map plans ({ path, timer, ... }: {
2020-09-08 21:45:42 +02:00
name = nameGenerator path;
value = {
wantedBy = [ "multi-user.target" ];
2021-11-01 09:20:42 +01:00
timerConfig.OnCalendar = timer;
2019-10-24 02:20:38 +02:00
};
2020-09-08 21:45:42 +02:00
}));
2019-10-24 02:20:38 +02:00
2021-11-01 09:20:42 +01:00
};
2019-10-24 02:20:38 +02:00
}