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
|
2022-10-18 08:42:24 +02:00
|
|
|
|
2020-09-08 21:45:42 +02:00
|
|
|
cfg = config.system.permown;
|
2022-10-18 08:42:24 +02:00
|
|
|
|
2020-09-08 21:45:42 +02:00
|
|
|
|
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;
|
|
|
|
};
|
2022-10-18 08:42:24 +02:00
|
|
|
keepGoing = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
|
|
|
Whether to keep going when chowning or chmodding fails.
|
|
|
|
If set to false, then errors will cause the service to restart
|
|
|
|
instead.
|
|
|
|
'';
|
|
|
|
};
|
2019-12-20 05:54:26 +01:00
|
|
|
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 =
|
2022-10-18 08:42:24 +02:00
|
|
|
let
|
|
|
|
plans = attrValues cfg;
|
|
|
|
in
|
|
|
|
mkIf (plans != [ ]) {
|
2021-11-01 09:20:42 +01:00
|
|
|
system.activationScripts.permown =
|
|
|
|
let
|
|
|
|
mkdir = { path, ... }: ''
|
2022-10-18 08:42:24 +02:00
|
|
|
${pkgs.coreutils}/bin/mkdir -p "${path}"
|
2021-11-01 09:20:42 +01:00
|
|
|
'';
|
|
|
|
in
|
|
|
|
concatMapStrings mkdir plans;
|
2019-10-24 02:20:38 +02:00
|
|
|
|
2022-10-18 08:42:24 +02:00
|
|
|
systemd.services =
|
|
|
|
let
|
|
|
|
nameGenerator = { path, ... }:
|
|
|
|
"permown.${replaceStrings [ "/" ] [ "_" ] path}";
|
|
|
|
serviceDefinition =
|
|
|
|
{ path, directory-mode, file-mode, owner, group, umask, keepGoing, ... }:
|
|
|
|
{
|
|
|
|
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 =
|
|
|
|
let
|
|
|
|
continuable = command:
|
|
|
|
if keepGoing
|
|
|
|
then "{ ${command}; } || :"
|
|
|
|
else command;
|
|
|
|
in
|
|
|
|
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" {} +
|
|
|
|
|
|
|
|
paths=/tmp/paths
|
|
|
|
rm -f "$paths"
|
|
|
|
mkfifo "$paths"
|
|
|
|
|
|
|
|
inotifywait -mrq -e CREATE --format %w%f "$ROOT_PATH" > "$paths" &
|
|
|
|
inotifywaitpid=$!
|
|
|
|
|
|
|
|
trap cleanup EXIT
|
|
|
|
cleanup() {
|
|
|
|
kill "$inotifywaitpid"
|
|
|
|
}
|
|
|
|
|
|
|
|
while read -r path
|
|
|
|
do
|
|
|
|
if test -d "$path"; then
|
|
|
|
cleanup
|
|
|
|
exec "$0" "$@"
|
|
|
|
fi
|
|
|
|
${continuable ''chown -h "$OWNER_GROUP" "$path"''}
|
|
|
|
if test -f "$path"; then
|
|
|
|
${continuable ''chmod "$FILE_MODE" "$path"''}
|
|
|
|
fi
|
|
|
|
done < "$paths"
|
|
|
|
'';
|
|
|
|
PrivateTmp = true;
|
|
|
|
Restart = "always";
|
|
|
|
RestartSec = 10;
|
|
|
|
UMask = umask;
|
|
|
|
};
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2021-11-01 09:20:42 +01:00
|
|
|
};
|
2022-10-18 08:42:24 +02:00
|
|
|
in
|
|
|
|
listToAttrs (map
|
|
|
|
(plan:
|
|
|
|
{
|
|
|
|
name = nameGenerator plan;
|
|
|
|
value = serviceDefinition plan;
|
|
|
|
})
|
|
|
|
plans);
|
2021-11-01 09:20:42 +01: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
|
|
|
|
|
|
|
}
|