Add mkOverlay
This commit is contained in:
parent
8b6484c870
commit
6fbd3729cc
4 changed files with 191 additions and 0 deletions
|
|
@ -3,4 +3,26 @@ let
|
||||||
in {
|
in {
|
||||||
init = pkgs: jailNix { inherit pkgs; };
|
init = pkgs: jailNix { inherit pkgs; };
|
||||||
extend = jailNix;
|
extend = jailNix;
|
||||||
|
|
||||||
|
mkOverlay = opts@{
|
||||||
|
final,
|
||||||
|
prev,
|
||||||
|
packages ? _: {},
|
||||||
|
...
|
||||||
|
}: let
|
||||||
|
fwdOptsKey = key: if opts ? ${key} then { ${key} = opts.${key}; } else {};
|
||||||
|
jail = jailNix (
|
||||||
|
{ pkgs = prev; }
|
||||||
|
// fwdOptsKey "additionalCombinators"
|
||||||
|
// fwdOptsKey "basePermissions"
|
||||||
|
// fwdOptsKey "bubblewrapPackage"
|
||||||
|
);
|
||||||
|
wrap = prev.lib.mapAttrs (name: opts:
|
||||||
|
let
|
||||||
|
unjailed = prev.${name};
|
||||||
|
jailed = jail name unjailed opts;
|
||||||
|
in jailed // { inherit jailed unjailed; }
|
||||||
|
);
|
||||||
|
in
|
||||||
|
wrap (packages jail.combinators);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ test-suite spec
|
||||||
CombinatorsSpec
|
CombinatorsSpec
|
||||||
DBusCombinatorsSpec
|
DBusCombinatorsSpec
|
||||||
JailSpec
|
JailSpec
|
||||||
|
OverlaySpec
|
||||||
TestPrelude
|
TestPrelude
|
||||||
Paths_jail_nix_tests
|
Paths_jail_nix_tests
|
||||||
hs-source-dirs:
|
hs-source-dirs:
|
||||||
|
|
|
||||||
123
tests/spec/OverlaySpec.hs
Normal file
123
tests/spec/OverlaySpec.hs
Normal file
|
|
@ -0,0 +1,123 @@
|
||||||
|
module OverlaySpec (spec) where
|
||||||
|
|
||||||
|
import Test.Hspec
|
||||||
|
import TestPrelude
|
||||||
|
|
||||||
|
spec :: Spec
|
||||||
|
spec = parallel $ inTestM $ do
|
||||||
|
let runOverlayTest :: String -> TestM String
|
||||||
|
runOverlayTest package = do
|
||||||
|
tmpDir <- getTestDir
|
||||||
|
liftIO $ do
|
||||||
|
writeFile (tmpDir </> "visible") "this file is visible to the jail\n"
|
||||||
|
writeFile (tmpDir </> "not-visible") "this file is not visible to the jail\n"
|
||||||
|
runNixDrv
|
||||||
|
[i|
|
||||||
|
let
|
||||||
|
pkgs = import <nixpkgs> { overlays = [ jailOverlay ]; };
|
||||||
|
jailOverlay = final: prev:
|
||||||
|
jail-nix.mkOverlay {
|
||||||
|
inherit final prev;
|
||||||
|
packages = combinators: with combinators; {
|
||||||
|
nodejs = [ (readonly #{toNixString $ tmpDir </> "visible"}) ];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
testProgram = ''
|
||||||
|
const fs = require("fs");
|
||||||
|
const logIfFileExists = file => console.log(file, fs.existsSync(file) ? "exists" : "doesn't exist");
|
||||||
|
logIfFileExists("visible");
|
||||||
|
logIfFileExists("not-visible");
|
||||||
|
'';
|
||||||
|
in sh ''
|
||||||
|
cd #{tmpDir}
|
||||||
|
${lib.getExe #{package}} -e ${lib.escapeShellArg testProgram}
|
||||||
|
''
|
||||||
|
|]
|
||||||
|
|
||||||
|
it "allows easily specifying a nixpkgs overlay" $ do
|
||||||
|
out <- runOverlayTest "pkgs.nodejs"
|
||||||
|
liftIO $ out `shouldBe` "visible exists\nnot-visible doesn't exist\n"
|
||||||
|
|
||||||
|
it "allows explicitly specifying that the package is jailed" $ do
|
||||||
|
out <- runOverlayTest "pkgs.nodejs.jailed"
|
||||||
|
liftIO $ out `shouldBe` "visible exists\nnot-visible doesn't exist\n"
|
||||||
|
|
||||||
|
it "exposes access to the unjailed package under `unjailed`" $ do
|
||||||
|
out <- runOverlayTest "pkgs.nodejs.unjailed"
|
||||||
|
liftIO $ out `shouldBe` "visible exists\nnot-visible exists\n"
|
||||||
|
|
||||||
|
it "supports specifying additional combinators" $ do
|
||||||
|
tmpDir <- getTestDir
|
||||||
|
liftIO $ writeFile (tmpDir </> "file") "some file"
|
||||||
|
[i|
|
||||||
|
let
|
||||||
|
pkgs = import <nixpkgs> { overlays = [ jailOverlay ]; };
|
||||||
|
jailOverlay = final: prev:
|
||||||
|
jail-nix.mkOverlay {
|
||||||
|
inherit final prev;
|
||||||
|
additionalCombinators = combinators: {
|
||||||
|
my-combinator = combinators.readonly #{toNixString $ tmpDir </> "file"};
|
||||||
|
};
|
||||||
|
packages = combinators: with combinators; {
|
||||||
|
nodejs = [ my-combinator ];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
in sh ''
|
||||||
|
cd #{tmpDir}
|
||||||
|
${lib.getExe pkgs.nodejs} -e "console.log(require('fs').readFileSync('file', { encoding: 'utf8' }))"
|
||||||
|
''
|
||||||
|
|]
|
||||||
|
`shouldOutput` "some file\n"
|
||||||
|
|
||||||
|
it "supports overriding base permissions" $ do
|
||||||
|
tmpDir <- getTestDir
|
||||||
|
liftIO $ writeFile (tmpDir </> "file") "some file"
|
||||||
|
[i|
|
||||||
|
let
|
||||||
|
pkgs = import <nixpkgs> { overlays = [ jailOverlay ]; };
|
||||||
|
jailOverlay = final: prev:
|
||||||
|
jail-nix.mkOverlay {
|
||||||
|
inherit final prev;
|
||||||
|
basePermissions = combinators: [
|
||||||
|
(combinators.readonly #{toNixString $ tmpDir </> "file"})
|
||||||
|
(combinators.readonly "/nix/store")
|
||||||
|
];
|
||||||
|
packages = combinators: with combinators; {
|
||||||
|
nodejs = [];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
in sh ''
|
||||||
|
cd #{tmpDir}
|
||||||
|
${lib.getExe pkgs.nodejs} -e "console.log(require('fs').readFileSync('file', { encoding: 'utf8' }))"
|
||||||
|
''
|
||||||
|
|]
|
||||||
|
`shouldOutput` "some file\n"
|
||||||
|
|
||||||
|
it "supports overriding the bublewrap package" $ do
|
||||||
|
tmpDir <- getTestDir
|
||||||
|
liftIO $ writeFile (tmpDir </> "file") "some file"
|
||||||
|
[i|
|
||||||
|
let
|
||||||
|
pkgs = import <nixpkgs> { overlays = [ jailOverlay ]; };
|
||||||
|
jailOverlay = final: prev:
|
||||||
|
jail-nix.mkOverlay {
|
||||||
|
inherit final prev;
|
||||||
|
bubblewrapPackage = pkgs.writeShellApplication {
|
||||||
|
name = "my-bubblewrap";
|
||||||
|
text = ''
|
||||||
|
# A version of bwrap that just prints "hello" instead of
|
||||||
|
# actually running a jail.
|
||||||
|
echo hello
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
basePermissions = null;
|
||||||
|
packages = combinators: with combinators; {
|
||||||
|
nodejs = [];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
in sh ''
|
||||||
|
cd #{tmpDir}
|
||||||
|
${lib.getExe pkgs.nodejs}
|
||||||
|
''
|
||||||
|
|]
|
||||||
|
`shouldOutput` "hello\n"
|
||||||
|
|
@ -91,6 +91,51 @@ following positional arguments:
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## Overlays
|
||||||
|
|
||||||
|
If you prefer, you can jail packages in a nixpkgs overlay using
|
||||||
|
`jail-nix.lib.mkOverlay`. This takes attribute set with `prev` and `final`
|
||||||
|
passed in, and `packages` which is a function from combinators to an attribute
|
||||||
|
set of package name to permissions.
|
||||||
|
|
||||||
|
All of the options from [Advanced Configuration](advanced-configuration.md) can
|
||||||
|
optionally be passed into `mkOverlay` attribute set as well.
|
||||||
|
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```nix
|
||||||
|
jailOverlay = final: prev: jail-nix.lib.mkOverlay {
|
||||||
|
inherit final prev;
|
||||||
|
packages = combinators: with combinators; {
|
||||||
|
nodejs = [ network ];
|
||||||
|
firefox = [ network gui gpu ];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
pkgs = import nixpkgs {
|
||||||
|
system = "x86_64-linux";
|
||||||
|
overlays = [ jailOverlay ];
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
This will then override the packages in `pkgs` with jailed versions, but also
|
||||||
|
expose `pkgs.«name».jailed` and `pkgs.«name».unjailed` on each overlaid
|
||||||
|
package. The `unjailed` is useful to get access to the original unjailed
|
||||||
|
package, and `jailed` is nice to use if you want to be explicit in the fact
|
||||||
|
that you are using the jailed package.
|
||||||
|
|
||||||
|
```nix
|
||||||
|
{
|
||||||
|
environment.systemPackages = [
|
||||||
|
pkgs.firefox # jailed
|
||||||
|
pkgs.firefox.jailed # jailed (explicitly)
|
||||||
|
pkgs.firefox.unjailed # unjailed
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
### Provide jail.nix as a module arg in a Nixos configuration:
|
### Provide jail.nix as a module arg in a Nixos configuration:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue