diff --git a/lib/default.nix b/lib/default.nix index 612d62e..389fce1 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -3,4 +3,26 @@ let in { init = pkgs: jailNix { inherit pkgs; }; 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); } diff --git a/tests/jail-nix-tests.cabal b/tests/jail-nix-tests.cabal index 32d4c82..2591e5e 100644 --- a/tests/jail-nix-tests.cabal +++ b/tests/jail-nix-tests.cabal @@ -15,6 +15,7 @@ test-suite spec CombinatorsSpec DBusCombinatorsSpec JailSpec + OverlaySpec TestPrelude Paths_jail_nix_tests hs-source-dirs: diff --git a/tests/spec/OverlaySpec.hs b/tests/spec/OverlaySpec.hs new file mode 100644 index 0000000..5c7c045 --- /dev/null +++ b/tests/spec/OverlaySpec.hs @@ -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 { 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 { 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 { 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 { 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" diff --git a/website/static-docs/index.md b/website/static-docs/index.md index 5441427..04bbc2e 100644 --- a/website/static-docs/index.md +++ b/website/static-docs/index.md @@ -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 ### Provide jail.nix as a module arg in a Nixos configuration: