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(-) (limited to 'krebs/3modules') diff --git a/krebs/3modules/permown.nix b/krebs/3modules/permown.nix index 7a86013e1..1e6471ede 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 From bc200e51552207a6d32caca8e57d6d39b06fe3c9 Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 18 Apr 2019 01:23:55 +0200 Subject: krebs.permown: mkdirs on activation --- krebs/3modules/permown.nix | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'krebs/3modules') diff --git a/krebs/3modules/permown.nix b/krebs/3modules/permown.nix index 1e6471ede..f190bf86a 100644 --- a/krebs/3modules/permown.nix +++ b/krebs/3modules/permown.nix @@ -37,6 +37,12 @@ with import ; plans = attrValues config.krebs.permown; in mkIf (plans != []) { + system.activationScripts.permown = let + mkdir = plan: /* sh */ '' + ${pkgs.coreutils}/bin/mkdir -p ${shell.escape plan.path} + ''; + in concatMapStrings mkdir plans; + systemd.services = genAttrs' plans (plan: { name = "permown.${replaceStrings ["/"] ["_"] plan.path}"; value = { -- cgit v1.2.3 From 87937a5394c15afced7f92dfce31a756bb7a4ae9 Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 18 Apr 2019 09:53:31 +0200 Subject: krebs.permown: [] -> {} --- krebs/3modules/permown.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'krebs/3modules') diff --git a/krebs/3modules/permown.nix b/krebs/3modules/permown.nix index f190bf86a..a3b49b303 100644 --- a/krebs/3modules/permown.nix +++ b/krebs/3modules/permown.nix @@ -35,7 +35,7 @@ with import ; config = let plans = attrValues config.krebs.permown; - in mkIf (plans != []) { + in mkIf (plans != {}) { system.activationScripts.permown = let mkdir = plan: /* sh */ '' -- cgit v1.2.3 From 3adcf3a74c00b5e88b8c8c15d6aeb9ab3f9304db Mon Sep 17 00:00:00 2001 From: lassulus Date: Thu, 18 Apr 2019 10:14:18 +0200 Subject: syncthing: listOf -> attrsOf --- krebs/3modules/syncthing.nix | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'krebs/3modules') diff --git a/krebs/3modules/syncthing.nix b/krebs/3modules/syncthing.nix index bfbac1db9..897ba1e7f 100644 --- a/krebs/3modules/syncthing.nix +++ b/krebs/3modules/syncthing.nix @@ -10,7 +10,7 @@ let addresses = peer.addresses; }) cfg.peers; - folders = map (folder: { + folders = mapAttrsToList ( _: folder: { inherit (folder) path id type; devices = map (peer: { deviceId = cfg.peers.${peer}.id; }) folder.peers; rescanIntervalS = folder.rescanInterval; @@ -81,17 +81,18 @@ in }; folders = mkOption { - default = []; - type = types.listOf (types.submodule ({ config, ... }: { + default = {}; + type = types.attrsOf (types.submodule ({ config, ... }: { options = { path = mkOption { type = types.absolute-pathname; + default = config._module.args.name; }; id = mkOption { type = types.str; - default = config.path; + default = config._module.args.name; }; peers = mkOption { -- cgit v1.2.3 From 64d6955e5a238016a1c6119516cb07caec4da4e5 Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 18 Apr 2019 10:19:10 +0200 Subject: Revert "krebs.permown: [] -> {}" This reverts commit 87937a5394c15afced7f92dfce31a756bb7a4ae9. Thanks for reviewing... --- krebs/3modules/permown.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'krebs/3modules') diff --git a/krebs/3modules/permown.nix b/krebs/3modules/permown.nix index a3b49b303..f190bf86a 100644 --- a/krebs/3modules/permown.nix +++ b/krebs/3modules/permown.nix @@ -35,7 +35,7 @@ with import ; config = let plans = attrValues config.krebs.permown; - in mkIf (plans != {}) { + in mkIf (plans != []) { system.activationScripts.permown = let mkdir = plan: /* sh */ '' -- cgit v1.2.3 From 1bbd53c4599fd1148bdb864f981b6fd4563fb476 Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 18 Apr 2019 11:00:56 +0200 Subject: krebs.permown: admit symlinks --- krebs/3modules/permown.nix | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'krebs/3modules') diff --git a/krebs/3modules/permown.nix b/krebs/3modules/permown.nix index f190bf86a..0f2ba86c8 100644 --- a/krebs/3modules/permown.nix +++ b/krebs/3modules/permown.nix @@ -61,7 +61,7 @@ with import ; ExecStart = pkgs.writeDash "permown" '' set -efu - find "$ROOT_PATH" -exec chown "$OWNER_GROUP" {} + + 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" {} + @@ -70,8 +70,10 @@ with import ; if test -d "$path"; then exec "$0" "$@" fi - chown "$OWNER_GROUP" "$path" - chmod "$FILE_MODE" "$path" + chown -h "$OWNER_GROUP" "$path" + if test -f "$path"; then + chmod "$FILE_MODE" "$path" + fi done ''; Restart = "always"; -- cgit v1.2.3 From c082c8d62be63c7acf31de37c4b87a5b5d8118fa Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 18 Apr 2019 11:31:19 +0200 Subject: krebs.permown: use named pipe This commit fixes following issues: 1. reexecution causes stray inotifywait processes 2. errors in the while part renderes the service defunct --- krebs/3modules/permown.nix | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'krebs/3modules') diff --git a/krebs/3modules/permown.nix b/krebs/3modules/permown.nix index 0f2ba86c8..63adb2236 100644 --- a/krebs/3modules/permown.nix +++ b/krebs/3modules/permown.nix @@ -65,17 +65,30 @@ with import ; 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" | + 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 chown -h "$OWNER_GROUP" "$path" if test -f "$path"; then chmod "$FILE_MODE" "$path" fi - done + done < "$paths" ''; + PrivateTemp = true; Restart = "always"; RestartSec = 10; UMask = plan.umask; -- cgit v1.2.3