2019-04-17 18:45:33 +00:00
|
|
|
with import <stockholm/lib>;
|
|
|
|
{ config, pkgs, ... }: {
|
|
|
|
|
|
|
|
options.krebs.permown = mkOption {
|
2019-04-17 23:23:12 +00:00
|
|
|
default = {};
|
|
|
|
type = types.attrsOf (types.submodule ({ config, ... }: {
|
2019-04-17 18:45:33 +00:00
|
|
|
options = {
|
|
|
|
directory-mode = mkOption {
|
|
|
|
default = "=rwx";
|
|
|
|
type = types.str; # TODO
|
|
|
|
};
|
|
|
|
file-mode = mkOption {
|
|
|
|
default = "=rw";
|
|
|
|
type = types.str; # TODO
|
|
|
|
};
|
|
|
|
group = mkOption {
|
|
|
|
apply = x: if x == null then "" else x;
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.groupname;
|
|
|
|
};
|
|
|
|
owner = mkOption {
|
|
|
|
type = types.username;
|
|
|
|
};
|
|
|
|
path = mkOption {
|
2019-04-17 23:23:12 +00:00
|
|
|
default = config._module.args.name;
|
2019-04-17 18:45:33 +00:00
|
|
|
type = types.absolute-pathname;
|
|
|
|
};
|
|
|
|
umask = mkOption {
|
|
|
|
default = "0027";
|
|
|
|
type = types.file-mode;
|
|
|
|
};
|
|
|
|
};
|
2019-04-17 23:23:12 +00:00
|
|
|
}));
|
2019-04-17 18:45:33 +00:00
|
|
|
};
|
|
|
|
|
2019-04-17 23:23:12 +00:00
|
|
|
config = let
|
|
|
|
plans = attrValues config.krebs.permown;
|
2019-04-18 08:19:10 +00:00
|
|
|
in mkIf (plans != []) {
|
2019-04-17 23:23:12 +00:00
|
|
|
|
2019-04-17 23:23:55 +00:00
|
|
|
system.activationScripts.permown = let
|
|
|
|
mkdir = plan: /* sh */ ''
|
|
|
|
${pkgs.coreutils}/bin/mkdir -p ${shell.escape plan.path}
|
|
|
|
'';
|
|
|
|
in concatMapStrings mkdir plans;
|
|
|
|
|
2019-04-17 23:23:12 +00:00
|
|
|
systemd.services = genAttrs' plans (plan: {
|
|
|
|
name = "permown.${replaceStrings ["/"] ["_"] plan.path}";
|
|
|
|
value = {
|
|
|
|
environment = {
|
|
|
|
DIR_MODE = plan.directory-mode;
|
|
|
|
FILE_MODE = plan.file-mode;
|
|
|
|
OWNER_GROUP = "${plan.owner}:${plan.group}";
|
|
|
|
ROOT_PATH = plan.path;
|
|
|
|
};
|
|
|
|
path = [
|
|
|
|
pkgs.coreutils
|
|
|
|
pkgs.findutils
|
|
|
|
pkgs.inotifyTools
|
|
|
|
];
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = pkgs.writeDash "permown" ''
|
|
|
|
set -efu
|
2019-04-17 18:45:33 +00:00
|
|
|
|
2019-04-18 09:00:56 +00:00
|
|
|
find "$ROOT_PATH" -exec chown -h "$OWNER_GROUP" {} +
|
2019-04-17 23:23:12 +00:00
|
|
|
find "$ROOT_PATH" -type d -exec chmod "$DIR_MODE" {} +
|
|
|
|
find "$ROOT_PATH" -type f -exec chmod "$FILE_MODE" {} +
|
2019-04-17 18:45:33 +00:00
|
|
|
|
2019-04-18 09:31:19 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2019-04-17 23:23:12 +00:00
|
|
|
while read -r path; do
|
|
|
|
if test -d "$path"; then
|
2019-04-18 09:31:19 +00:00
|
|
|
cleanup
|
2019-04-17 23:23:12 +00:00
|
|
|
exec "$0" "$@"
|
|
|
|
fi
|
2019-04-18 09:00:56 +00:00
|
|
|
chown -h "$OWNER_GROUP" "$path"
|
|
|
|
if test -f "$path"; then
|
|
|
|
chmod "$FILE_MODE" "$path"
|
|
|
|
fi
|
2019-04-18 09:31:19 +00:00
|
|
|
done < "$paths"
|
2019-04-17 23:23:12 +00:00
|
|
|
'';
|
2019-04-19 11:58:38 +00:00
|
|
|
PrivateTmp = true;
|
2019-04-17 23:23:12 +00:00
|
|
|
Restart = "always";
|
|
|
|
RestartSec = 10;
|
|
|
|
UMask = plan.umask;
|
|
|
|
};
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2019-04-17 18:45:33 +00:00
|
|
|
};
|
2019-04-17 23:23:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
};
|
2019-04-17 18:45:33 +00:00
|
|
|
|
|
|
|
}
|