Add experimental overlay combinators

This commit is contained in:
Alex David 2026-04-14 22:26:43 -07:00
commit 404e7da9da
4 changed files with 191 additions and 0 deletions

View file

@ -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}" ]
)
);
}

View file

@ -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}" ]
)
);
}

View file

@ -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}" ]
)
);
}

View file

@ -284,6 +284,94 @@ spec = parallel $ inTestM $ do
|] |]
liftIO $ doesFileExist (tmpDir </> "fake-browser") `shouldReturn` False 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 describe "readonly-paths-from-var" $ do
forM_ [":", " "] $ \separator -> forM_ [":", " "] $ \separator ->
describe ("with separator \"" <> separator <> "\"") $ do describe ("with separator \"" <> separator <> "\"") $ do