diff --git a/combinators.nix b/combinators.nix index 311f2da..3b4aa52 100644 --- a/combinators.nix +++ b/combinators.nix @@ -479,6 +479,42 @@ ; }; + readonly-paths-from-var = { + sig = "String -> String -> Combinator"; + doc = '' + This binds multiple paths as read-only specified by a single runtime + environment variable. + + The first argument to this combinator is the runtime environment variable + that contains a list of paths to be bound. The second argument is a + deliminator to split the paths (Typically either `" "` or `":"`). + + This is useful for variables like `XDG_DATA_DIRS`, `GTK_PATH`, + `XCURSOR_PATH`, etc. + + Example: + ```nix + compose [ + (readonly-paths-from-var "XDG_DATA_DIRS" ":") + (readonly-paths-from-var "XCURSOR_PATH" " ") + ] + ``` + ''; + __functor = _: + var: separator: + assert pkgs.lib.isValidPosixName var; + include-once "readonly-paths-from-var-${var}" + (add-runtime '' + while read -rd${lib.escapeShellArg separator} DIR; do + if [ -e "$DIR" ]; then + P="$(realpath "$DIR")" + RUNTIME_ARGS+=(--ro-bind "$P" "$P") + fi + done <<< "${"$"}${var}" + '') + ; + }; + mount-cwd = { sig = "Combinator"; doc = '' @@ -504,15 +540,17 @@ pulse pipewire wayland - (fwd-env "XDG_RUNTIME_DIR") (readonly (noescape "/etc/fonts")) (readonly (noescape "~/.config/dconf")) + (fwd-env "XDG_RUNTIME_DIR") + (fwd-env "XDG_DATA_DIRS") + (readonly-paths-from-var "XDG_DATA_DIRS" ":") # Cursor (fwd-env "XCURSOR_THEME") (fwd-env "XCURSOR_PATH") (fwd-env "XCURSOR_SIZE") - (readonly (noescape "/etc/profiles/per-user/\"$USER\"/share/icons")) # TODO - this is from XCURSOR_PATH, maybe readonly these paths? + (readonly-paths-from-var "XCURSOR_PATH" " ") ] ; }; diff --git a/tests/test-cases.nix b/tests/test-cases.nix index 6861246..fb8a289 100644 --- a/tests/test-cases.nix +++ b/tests/test-cases.nix @@ -58,6 +58,36 @@ in { ) ]; + "combinators/readonly-from-path-var" = let + separator = " "; + mkTestPath = path: "mkdir -p /build/${path} && touch /build/${path}/file"; + in [ + (mkTestPath "some/path") + (mkTestPath "some/other/path") + (mkTestPath "some/not/in/var") + "export TEST_PATHS=${lib.escapeShellArg (lib.concatStringsSep separator [ + "some/path" + "some/non/existant/path" + "some/other/path" + "some/other/non/existant/path" + ])}" + (assertStdout + (jail "test" (sh "${lib.getExe pkgs.tree} /build/some") (c: [ + (c.readonly-paths-from-var "TEST_PATHS" separator) + ])) + (lib.trim '' + /build/some + |-- other + | `-- path + | `-- file + `-- path + `-- file + + 4 directories, 2 files + '') + ) + ]; + "combinators/set-argv" = assertStdout (jail "test" (sh "printf '1=%s,2=%s,3=%s' \"$@\"") (c: [ (c.set-argv [ "foo" "bar baz" "foo>'bar'" ])