Add readonly-paths-from-var combinator

This commit is contained in:
Alex David 2025-07-18 11:30:55 -07:00
commit ba42171b16
2 changed files with 70 additions and 2 deletions

View file

@ -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 = { mount-cwd = {
sig = "Combinator"; sig = "Combinator";
doc = '' doc = ''
@ -504,15 +540,17 @@
pulse pulse
pipewire pipewire
wayland wayland
(fwd-env "XDG_RUNTIME_DIR")
(readonly (noescape "/etc/fonts")) (readonly (noescape "/etc/fonts"))
(readonly (noescape "~/.config/dconf")) (readonly (noescape "~/.config/dconf"))
(fwd-env "XDG_RUNTIME_DIR")
(fwd-env "XDG_DATA_DIRS")
(readonly-paths-from-var "XDG_DATA_DIRS" ":")
# Cursor # Cursor
(fwd-env "XCURSOR_THEME") (fwd-env "XCURSOR_THEME")
(fwd-env "XCURSOR_PATH") (fwd-env "XCURSOR_PATH")
(fwd-env "XCURSOR_SIZE") (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" " ")
] ]
; ;
}; };

View file

@ -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 "combinators/set-argv" = assertStdout
(jail "test" (sh "printf '1=%s,2=%s,3=%s' \"$@\"") (c: [ (jail "test" (sh "printf '1=%s,2=%s,3=%s' \"$@\"") (c: [
(c.set-argv [ "foo" "bar baz" "foo>'bar'" ]) (c.set-argv [ "foo" "bar baz" "foo>'bar'" ])