Add try-* bind combinators

This commit is contained in:
Alex David 2025-09-27 10:51:19 -07:00
commit 3ccf1d42e2
2 changed files with 170 additions and 0 deletions

View file

@ -525,6 +525,9 @@ rec {
sig = "String -> Permission"; sig = "String -> Permission";
doc = '' doc = ''
Binds the specified path in the jail as read-only. Binds the specified path in the jail as read-only.
This will error if the file does not exist. If you want to be tolerant of
missing files, see [try-readonly](#try-readonly).
''; '';
__functor = _: path: ro-bind path path; __functor = _: path: ro-bind path path;
}; };
@ -533,6 +536,9 @@ rec {
sig = "String -> Permission"; sig = "String -> Permission";
doc = '' doc = ''
Binds the specified path in the jail as read-write. Binds the specified path in the jail as read-write.
This will error if the file does not exist. If you want to be tolerant of
missing files, see [try-readwrite](#try-readwrite).
''; '';
__functor = _: path: rw-bind path path; __functor = _: path: rw-bind path path;
}; };
@ -542,6 +548,9 @@ rec {
doc = '' doc = ''
Binds the specified path on the host to a path in the jail as read-only. Binds the specified path on the host to a path in the jail as read-only.
This will error if the host file does not exist. If you want to be
tolerant of missing files, see [try-ro-bind](#try-ro-bind).
Example: Example:
```nix ```nix
# Binds /foo on the host to /bar in the jail # Binds /foo on the host to /bar in the jail
@ -558,6 +567,9 @@ rec {
doc = '' doc = ''
Binds the specified path on the host to a path in the jail as read-write. Binds the specified path on the host to a path in the jail as read-write.
This will error if the host file does not exist. If you want to be
tolerant of missing files, see [try-rw-bind](#try-rw-bind).
Example: Example:
```nix ```nix
# Binds /foo on the host to /bar in the jail # Binds /foo on the host to /bar in the jail
@ -569,6 +581,56 @@ rec {
unsafe-add-raw-args "--bind ${escape from} ${escape to}"; unsafe-add-raw-args "--bind ${escape from} ${escape to}";
}; };
try-readonly = {
sig = "String -> Permission";
doc = ''
Binds the specified path in the jail as read-only if it exists.
'';
__functor = _: path: try-ro-bind path path;
};
try-readwrite = {
sig = "String -> Permission";
doc = ''
Binds the specified path in the jail as read-write if it exists.
'';
__functor = _: path: try-rw-bind path path;
};
try-ro-bind = {
sig = "String -> String -> Permission";
doc = ''
Binds the specified path on the host to a path in the jail as read-only
if it exists.
Example:
```nix
# Binds /foo on the host to /bar in the jail if /foo exists
try-ro-bind "/foo" "/bar"
```
'';
__functor =
_: from: to:
unsafe-add-raw-args "--ro-bind-try ${escape from} ${escape to}";
};
try-rw-bind = {
sig = "String -> String -> Permission";
doc = ''
Binds the specified path on the host to a path in the jail as read-write
if it exists.
Example:
```nix
# Binds /foo on the host to /bar in the jail if /foo exists
try-rw-bind "/foo" "/bar"
```
'';
__functor =
_: from: to:
unsafe-add-raw-args "--bind-try ${escape from} ${escape to}";
};
readonly-runtime-args = { readonly-runtime-args = {
sig = "Permission"; sig = "Permission";
doc = '' doc = ''

View file

@ -238,3 +238,111 @@ spec = parallel $ inTestM $ do
) )
|] |]
`shouldOutput` "EST\n" `shouldOutput` "EST\n"
describe "try-readonly" $ do
it "binds paths as readonly in the jail" $ do
tmpDir <- getTestDir
liftIO $ writeFile (tmpDir </> "my-file") "foo"
[i|
jail "test" (sh ''cat "#{tmpDir </> "my-file"}"'') (c: [
(c.try-readonly "#{tmpDir </> "my-file"}")
])
|]
`shouldOutput` "foo"
out <-
runNixDrv
[i|
jail "test" (sh ''echo new-contents | tee "#{tmpDir </> "my-file"}" 2>&1 || true'') (c: [
(c.try-readonly "#{tmpDir </> "my-file"}")
])
|]
liftIO $ out `shouldContain` "my-file: Read-only file system"
it "does not error if the file does not exist" $ do
[i|
jail "test" (sh ''echo ok'') (c: [
(c.try-readonly "/this/path/does/not/exist")
])
|]
`shouldOutput` "ok\n"
describe "try-readwrite" $ do
it "binds paths as readwrite in the jail" $ do
tmpDir <- getTestDir
liftIO $ writeFile (tmpDir </> "my-file") "foo"
[i|
jail "test" (sh ''cat "#{tmpDir </> "my-file"}"'') (c: [
(c.try-readwrite "#{tmpDir </> "my-file"}")
])
|]
`shouldOutput` "foo"
void $
runNixDrv
[i|
jail "test" (sh ''echo new-contents | tee "#{tmpDir </> "my-file"}"'') (c: [
(c.try-readwrite "#{tmpDir </> "my-file"}")
])
|]
liftIO $ readFile (tmpDir </> "my-file") `shouldReturn` "new-contents\n"
it "does not error if the file does not exist" $ do
[i|
jail "test" (sh ''echo ok'') (c: [
(c.try-readwrite "/this/path/does/not/exist")
])
|]
`shouldOutput` "ok\n"
describe "try-ro-bind" $ do
it "binds paths as readonly in the jail" $ do
tmpDir <- getTestDir
liftIO $ writeFile (tmpDir </> "my-file") "foo"
[i|
jail "test" (sh ''cat /path/in/jail'') (c: [
(c.try-ro-bind "#{tmpDir </> "my-file"}" "/path/in/jail")
])
|]
`shouldOutput` "foo"
out <-
runNixDrv
[i|
jail "test" (sh ''echo new-contents | tee /path/in/jail 2>&1 || true'') (c: [
(c.try-ro-bind "#{tmpDir </> "my-file"}" "/path/in/jail")
])
|]
liftIO $ out `shouldContain` "/path/in/jail: Read-only file system"
it "does not error if the file does not exist" $ do
[i|
jail "test" (sh ''echo ok'') (c: [
(c.try-ro-bind "/this/path/does/not/exist" "/path/in/jail")
])
|]
`shouldOutput` "ok\n"
describe "try-rw-bind" $ do
it "binds paths as readwrite in the jail" $ do
tmpDir <- getTestDir
liftIO $ writeFile (tmpDir </> "my-file") "foo"
[i|
jail "test" (sh ''cat /path/in/jail'') (c: [
(c.try-rw-bind "#{tmpDir </> "my-file"}" "/path/in/jail")
])
|]
`shouldOutput` "foo"
void $
runNixDrv
[i|
jail "test" (sh ''echo new-contents | tee /path/in/jail'') (c: [
(c.try-rw-bind "#{tmpDir </> "my-file"}" "/path/in/jail")
])
|]
liftIO $ readFile (tmpDir </> "my-file") `shouldReturn` "new-contents\n"
it "does not error if the file does not exist" $ do
[i|
jail "test" (sh ''echo ok'') (c: [
(c.try-rw-bind "/this/path/does/not/exist" "/path/in/jail")
])
|]
`shouldOutput` "ok\n"