From 520c9ef692d07672aa61c9e69bf34065f5abfbe1 Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 18 Apr 2019 01:23:12 +0200 Subject: krebs.permown: listOf -> attrsOf --- krebs/3modules/permown.nix | 83 +++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/krebs/3modules/permown.nix b/krebs/3modules/permown.nix index 7a86013e..1e6471ed 100644 --- a/krebs/3modules/permown.nix +++ b/krebs/3modules/permown.nix @@ -2,8 +2,8 @@ with import ; { config, pkgs, ... }: { options.krebs.permown = mkOption { - default = []; - type = types.listOf (types.submodule { + default = {}; + type = types.attrsOf (types.submodule ({ config, ... }: { options = { directory-mode = mkOption { default = "=rwx"; @@ -22,6 +22,7 @@ with import ; type = types.username; }; path = mkOption { + default = config._module.args.name; type = types.absolute-pathname; }; umask = mkOption { @@ -29,46 +30,52 @@ with import ; type = types.file-mode; }; }; - }); + })); }; - config.systemd.services = genAttrs' config.krebs.permown (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 + config = let + plans = attrValues config.krebs.permown; + in mkIf (plans != []) { + + 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 - find "$ROOT_PATH" -exec chown "$OWNER_GROUP" {} + - find "$ROOT_PATH" -type d -exec chmod "$DIR_MODE" {} + - find "$ROOT_PATH" -type f -exec chmod "$FILE_MODE" {} + + find "$ROOT_PATH" -exec chown "$OWNER_GROUP" {} + + find "$ROOT_PATH" -type d -exec chmod "$DIR_MODE" {} + + find "$ROOT_PATH" -type f -exec chmod "$FILE_MODE" {} + - inotifywait -mrq -e CREATE --format %w%f "$ROOT_PATH" | - while read -r path; do - if test -d "$path"; then - exec "$0" "$@" - fi - chown "$OWNER_GROUP" "$path" - chmod "$FILE_MODE" "$path" - done - ''; - Restart = "always"; - RestartSec = 10; - UMask = plan.umask; + inotifywait -mrq -e CREATE --format %w%f "$ROOT_PATH" | + while read -r path; do + if test -d "$path"; then + exec "$0" "$@" + fi + chown "$OWNER_GROUP" "$path" + chmod "$FILE_MODE" "$path" + done + ''; + Restart = "always"; + RestartSec = 10; + UMask = plan.umask; + }; + wantedBy = [ "multi-user.target" ]; }; - wantedBy = [ "multi-user.target" ]; - }; - }); + }); + + }; } -- cgit v1.2.3