From 3ccf1d42e25b92e12f52ac7ad01082cd23c7456d Mon Sep 17 00:00:00 2001 From: Alex David Date: Sat, 27 Sep 2025 10:51:19 -0700 Subject: [PATCH] Add `try-*` bind combinators --- lib/combinators.nix | 62 +++++++++++++++++++ tests/spec/CombinatorsSpec.hs | 108 ++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+) diff --git a/lib/combinators.nix b/lib/combinators.nix index d130757..ea3a15c 100644 --- a/lib/combinators.nix +++ b/lib/combinators.nix @@ -525,6 +525,9 @@ rec { sig = "String -> Permission"; doc = '' 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; }; @@ -533,6 +536,9 @@ rec { sig = "String -> Permission"; doc = '' 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; }; @@ -542,6 +548,9 @@ rec { doc = '' 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: ```nix # Binds /foo on the host to /bar in the jail @@ -558,6 +567,9 @@ rec { doc = '' 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: ```nix # Binds /foo on the host to /bar in the jail @@ -569,6 +581,56 @@ rec { 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 = { sig = "Permission"; doc = '' diff --git a/tests/spec/CombinatorsSpec.hs b/tests/spec/CombinatorsSpec.hs index 7708310..d9b7685 100644 --- a/tests/spec/CombinatorsSpec.hs +++ b/tests/spec/CombinatorsSpec.hs @@ -238,3 +238,111 @@ spec = parallel $ inTestM $ do ) |] `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"