From 404e7da9da5ab9aa643666682b2ba1312fa5fbe8 Mon Sep 17 00:00:00 2001 From: Alex David Date: Tue, 14 Apr 2026 22:26:43 -0700 Subject: [PATCH] Add experimental overlay combinators --- lib/combinators/overlay-ro.nix | 32 ++++++++++++ lib/combinators/overlay-rw.nix | 39 +++++++++++++++ lib/combinators/overlay-tmp.nix | 32 ++++++++++++ tests/spec/CombinatorsSpec.hs | 88 +++++++++++++++++++++++++++++++++ 4 files changed, 191 insertions(+) create mode 100644 lib/combinators/overlay-ro.nix create mode 100644 lib/combinators/overlay-rw.nix create mode 100644 lib/combinators/overlay-tmp.nix diff --git a/lib/combinators/overlay-ro.nix b/lib/combinators/overlay-ro.nix new file mode 100644 index 0000000..cbb5251 --- /dev/null +++ b/lib/combinators/overlay-ro.nix @@ -0,0 +1,32 @@ +{ combinators, ... }: +let + inherit (combinators) escape unsafe-add-raw-args; +in +{ + experimental = true; + sig = "[String] -> String -> Permission"; + doc = '' + Creates a readonly overlay fs mount at a destination. + + The first argument is a list of sources to be composed, the second is the + destination in the jail where to mount the overlayfs. + + If multiple sources contain the same files, sources specified later in the + sources list will shadow earlier ones. + + At least one source must be specified. + + Example: + ```nix + overlay-ro ["/source/foo" "/source/bar"] "/dest" + ``` + ''; + impl = + sources: dest: + assert builtins.length sources > 0; + unsafe-add-raw-args ( + builtins.concatStringsSep " " ( + map (source: "--overlay-src ${escape source}") sources ++ [ "--ro-overlay ${escape dest}" ] + ) + ); +} diff --git a/lib/combinators/overlay-rw.nix b/lib/combinators/overlay-rw.nix new file mode 100644 index 0000000..d327f3f --- /dev/null +++ b/lib/combinators/overlay-rw.nix @@ -0,0 +1,39 @@ +{ combinators, ... }: +let + inherit (combinators) escape unsafe-add-raw-args; +in +{ + experimental = true; + sig = "[String] -> String -> String -> String -> Permission"; + doc = '' + Creates an overlay fs mount at a destination. + + The first argument is a list of sources to be composed. + + The second argument is the rw source, a directory to persist writes to. + + The third argument is the workdir, a scratch space the kernel uses for the + overlayFS (it must be on the same filesystem as the rw source). + + The final argument is the destination in the jail where to mount the overlayfs. + + If multiple sources contain the same files, sources specified later in the + sources list will shadow earlier ones. + + At least one source must be specified. + + Example: + ```nix + overlay-rw ["/source/foo" "/source/bar"] "/tmp/rw" "/tmp/workdir" "/dest" + ``` + ''; + impl = + sources: rwSource: workdir: dest: + assert builtins.length sources > 0; + unsafe-add-raw-args ( + builtins.concatStringsSep " " ( + map (source: "--overlay-src ${escape source}") sources + ++ [ "--overlay ${escape rwSource} ${escape workdir} ${escape dest}" ] + ) + ); +} diff --git a/lib/combinators/overlay-tmp.nix b/lib/combinators/overlay-tmp.nix new file mode 100644 index 0000000..b15ac8f --- /dev/null +++ b/lib/combinators/overlay-tmp.nix @@ -0,0 +1,32 @@ +{ combinators, ... }: +let + inherit (combinators) escape unsafe-add-raw-args; +in +{ + experimental = true; + sig = "[String] -> String -> Permission"; + doc = '' + Creates a tempfs overlay mount at a destination. + + The first argument is a list of sources to be composed, the second is the + destination in the jail where to mount the overlayfs. + + If multiple sources contain the same files, sources specified later in the + sources list will shadow earlier ones. + + At least one source must be specified. + + Example: + ```nix + overlay-tmp ["/source/foo" "/source/bar"] "/dest" + ``` + ''; + impl = + sources: dest: + assert builtins.length sources > 0; + unsafe-add-raw-args ( + builtins.concatStringsSep " " ( + map (source: "--overlay-src ${escape source}") sources ++ [ "--tmp-overlay ${escape dest}" ] + ) + ); +} diff --git a/tests/spec/CombinatorsSpec.hs b/tests/spec/CombinatorsSpec.hs index 830f796..996928f 100644 --- a/tests/spec/CombinatorsSpec.hs +++ b/tests/spec/CombinatorsSpec.hs @@ -284,6 +284,94 @@ spec = parallel $ inTestM $ do |] liftIO $ doesFileExist (tmpDir "fake-browser") `shouldReturn` False + describe "overlay combinators" $ do + let runOverlayTest :: String -> String -> TestM String + runOverlayTest combinatorInvocation shellCmd = do + tmpDir <- getTestDir + let fooSource = tmpDir "source/foo" + let barSource = tmpDir "source/bar" + let rwSource = tmpDir "rwSource" + let workDir = tmpDir "workDir" + liftIO $ do + createDirectoryIfMissing True fooSource + createDirectoryIfMissing True barSource + createDirectoryIfMissing True rwSource + createDirectoryIfMissing True workDir + writeFile (fooSource "only-foo") "only-foo" + writeFile (barSource "only-bar") "only-bar" + writeFile (fooSource "both") "both-foo" + writeFile (barSource "both") "both-bar" + runNixDrv + [i| + let + fooSource = "#{fooSource}"; + barSource = "#{barSource}"; + rwSource = "#{rwSource}"; + workDir = "#{workDir}"; + in + jail "test" (sh "#{shellCmd}") (c: [ + (#{combinatorInvocation}) + ]) + |] + + describe "overlay-tmp" $ do + let runTest = runOverlayTest [i| c.overlay-tmp [ fooSource barSource ] "/dest" |] + it "overlays files from the sources list" $ do + runTest "ls /dest" + `shouldReturnTestM` unindent + [i| + both + only-bar + only-foo + |] + + it "uses sources specified later to shadow files specified in multiple sources" $ do + runTest "cat /dest/both" + `shouldReturnTestM` "both-bar" + + it "creates a writable tmpfs overlay" $ do + runTest "echo baz > /dest/baz && cat /dest/baz" + `shouldReturnTestM` "baz\n" + + describe "overlay-ro" $ do + let runTest = runOverlayTest [i| c.overlay-ro [ fooSource barSource ] "/dest" |] + it "overlays files from the sources list" $ do + runTest "ls /dest" + `shouldReturnTestM` unindent + [i| + both + only-bar + only-foo + |] + + it "uses sources specified later to shadow files specified in multiple sources" $ do + runTest "cat /dest/both" + `shouldReturnTestM` "both-bar" + + it "mounts a read-only destination" $ do + runTest "echo baz > /dest/baz || echo failed" + `shouldReturnTestM` "failed\n" + + describe "overlay-rw" $ do + let runTest = runOverlayTest [i| c.overlay-rw [ fooSource barSource ] rwSource workDir "/dest" |] + it "overlays files from the sources list" $ do + runTest "ls /dest" + `shouldReturnTestM` unindent + [i| + both + only-bar + only-foo + |] + + it "uses sources specified later to shadow files specified in multiple sources" $ do + runTest "cat /dest/both" + `shouldReturnTestM` "both-bar" + + it "writes chagnes to the rw directory" $ do + void $ runTest "echo baz > /dest/baz" + tmpDir <- getTestDir + liftIO $ readFile (tmpDir "rwSource/baz") `shouldReturn` "baz\n" + describe "readonly-paths-from-var" $ do forM_ [":", " "] $ \separator -> describe ("with separator \"" <> separator <> "\"") $ do