Compare commits

..

No commits in common. "a057ce75e9c06fcd24477a29b4c9f8f293e9e5b4" and "42b355c38ca63dab4904acc5c0d95f17954a8c9b" have entirely different histories.

19 changed files with 142 additions and 309 deletions

View file

@ -1,4 +1,4 @@
image: nixos/26.05
image: nixos/25.05
environment:
NIX_CONFIG: "experimental-features = nix-command flakes"
secrets:

View file

@ -1 +1 @@
_: { lib = import ./lib; }
_: import ./lib

View file

@ -1,51 +1,107 @@
{
pkgs,
suppressExperimentalWarnings ? false,
...
}:
jail:
pkgs: jail:
let
inherit (pkgs) lib;
helpers = import ./helpers.nix pkgs;
rawCombinators = lib.mapAttrs' (
fileName: _:
lib.nameValuePair (lib.removeSuffix ".nix" fileName) (
import ./combinators/${fileName} {
inherit
combinators
helpers
jail
lib
pkgs
;
rawCombinators =
lib.mapAttrs
(
_: file:
import file {
inherit
combinators
helpers
jail
lib
pkgs
;
}
)
{
add-cleanup = ./combinators/add-cleanup.nix;
add-path = ./combinators/add-path.nix;
add-pkg-deps = ./combinators/add-pkg-deps.nix;
add-runtime = ./combinators/add-runtime.nix;
add-seccomp = ./combinators/add-seccomp.nix;
base = ./combinators/base.nix;
bind-nix-store-runtime-closure = ./combinators/bind-nix-store-runtime-closure.nix;
bind-pkg = ./combinators/bind-pkg.nix;
camera = ./combinators/camera.nix;
compose = ./combinators/compose.nix;
dbus = ./combinators/dbus.nix;
defer = ./combinators/defer.nix;
escape = ./combinators/escape.nix;
fake-passwd = ./combinators/fake-passwd.nix;
fwd-env = ./combinators/fwd-env.nix;
gpu = ./combinators/gpu.nix;
gui = ./combinators/gui.nix;
include-once = ./combinators/include-once.nix;
jail-to-host-channel = ./combinators/jail-to-host-channel.nix;
mount-cwd = ./combinators/mount-cwd.nix;
network = ./combinators/network.nix;
no-die-with-parent = ./combinators/no-die-with-parent.nix;
no-new-session = ./combinators/no-new-session.nix;
noescape = ./combinators/noescape.nix;
notifications = ./combinators/notifications.nix;
open-urls-in-browser = ./combinators/open-urls-in-browser.nix;
persist-home = ./combinators/persist-home.nix;
pipewire = ./combinators/pipewire.nix;
pulse = ./combinators/pulse.nix;
readonly = ./combinators/readonly.nix;
readonly-paths-from-var = ./combinators/readonly-paths-from-var.nix;
readonly-runtime-args = ./combinators/readonly-runtime-args.nix;
readwrite = ./combinators/readwrite.nix;
readwrite-runtime-args = ./combinators/readwrite-runtime-args.nix;
reset = ./combinators/reset.nix;
ro-bind = ./combinators/ro-bind.nix;
runtime-deep-ro-bind = ./combinators/runtime-deep-ro-bind.nix;
rw-bind = ./combinators/rw-bind.nix;
set-argv = ./combinators/set-argv.nix;
set-env = ./combinators/set-env.nix;
set-hostname = ./combinators/set-hostname.nix;
share-ns = ./combinators/share-ns.nix;
time-zone = ./combinators/time-zone.nix;
tmpfs = ./combinators/tmpfs.nix;
try-fwd-env = ./combinators/try-fwd-env.nix;
try-readonly = ./combinators/try-readonly.nix;
try-readwrite = ./combinators/try-readwrite.nix;
try-ro-bind = ./combinators/try-ro-bind.nix;
try-rw-bind = ./combinators/try-rw-bind.nix;
unsafe-add-raw-args = ./combinators/unsafe-add-raw-args.nix;
unsafe-dbus = ./combinators/unsafe-dbus.nix;
unsafe-x11 = ./combinators/unsafe-x11.nix;
wayland = ./combinators/wayland.nix;
wrap-entry = ./combinators/wrap-entry.nix;
write-text = ./combinators/write-text.nix;
xwayland = ./combinators/xwayland.nix;
}
)
) (builtins.readDir ./combinators);
//
############################################
# deprecated combinators:
{
persisthome = {
deprecated = true;
sig = "Permission";
doc = "This was reworked to store data under `~/.local/share/jail.nix` and renamed to [persist-home](#persist-home).";
impl =
with combinators;
name:
lib.warn
"persisthome is deprecated, use persist-home instead. When doing so, rename ~/.local/share/jails/${name} to ${helpers.dataDirSubPath "home/${name}"}"
(compose [
(add-runtime "mkdir -p ~/.local/share/jails/${lib.escapeShellArg name}")
(rw-bind (noescape "~/.local/share/jails/${lib.escapeShellArg name}") (noescape "~"))
]);
};
combinators =
let
foldCombinator =
acc: names: combinator:
if lib.length names == 0 then
acc
else
let
name = lib.head names;
combinatorImpl =
if combinator.experimental or false && !suppressExperimentalWarnings then
builtins.warn "jail.nix combinator ${name} is experimental!" combinator.impl
else
combinator.impl;
in
if acc ? ${name} then
throw "duplicate combinator ${name}"
else
foldCombinator (acc // { ${name} = combinatorImpl; }) (lib.tail names) combinator;
in
lib.foldl' (acc: { name, value }: foldCombinator acc ([ name ] ++ value.aliases or [ ]) value) { } (
lib.attrsToList rawCombinators
);
dbus-unsafe = {
deprecated = true;
sig = "Permission";
doc = "This was renamed to [unsafe-dbus](#unsafe-dbus).";
impl = lib.warn "dbus-unsafe is deprecated, use unsafe-dbus instead" combinators.unsafe-dbus;
};
};
combinators = lib.mapAttrs (_: combinatorObj: combinatorObj.impl) rawCombinators;
in
{
inherit combinators;

View file

@ -1,32 +0,0 @@
{ 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

@ -1,39 +0,0 @@
{ 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

@ -1,32 +0,0 @@
{ 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

@ -31,10 +31,11 @@ in
assert lib.isValidPosixName var;
include-once "readonly-paths-from-var-${var}" (add-runtime ''
IFS=${lib.escapeShellArg separator} read -ra DIRS <<< "''${${var}-}"
if ((''${#DIRS[@]})); then
while IFS= read -r -d ''' P; do
for DIR in "''${DIRS[@]}"; do
if [ -e "$DIR" ]; then
P="$(realpath "$DIR")"
RUNTIME_ARGS+=(--ro-bind "$P" "$P")
done < <(realpath -ezq -- "''${DIRS[@]}")
fi
fi
done
'');
}

View file

@ -1,14 +1,13 @@
jailNixCfg@{
{
pkgs,
additionalCombinators ? _: { },
basePermissions ? import ./base-permissions.nix pkgs,
bubblewrapPackage ? pkgs.bubblewrap,
...
}:
let
inherit (pkgs) lib;
builtinCombinators = (import ./combinators.nix jailNixCfg jail).combinators;
builtinCombinators = (import ./combinators.nix pkgs jail).combinators;
allCombinators = builtinCombinators // additionalCombinators builtinCombinators;

8
tests/flake.lock generated
View file

@ -2,16 +2,16 @@
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1783389287,
"narHash": "sha256-0xIy4dVLqq47rA+mRy0hXDfjhQd4E5PoIns/RmB7nR4=",
"lastModified": 1769318308,
"narHash": "sha256-Mjx6p96Pkefks3+aA+72lu1xVehb6mv2yTUUqmSet6Q=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "0ad6f47ea4fe188f4bc8f0380f93ae8523337c6c",
"rev": "1cd347bf3355fce6c64ab37d3967b4a2cb4b878c",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-26.05",
"ref": "nixos-25.11",
"repo": "nixpkgs",
"type": "github"
}

View file

@ -1,7 +1,7 @@
{
# This is a separate flake file from the one at the root because we don't
# want to depend on nixpkgs
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-26.05";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
outputs =
{ nixpkgs, ... }:
let

View file

@ -152,7 +152,7 @@ spec = parallel $ inTestM $ do
runNixDrv
[i|
let
jail' = jail-nix.lib.extend {
jail' = jail-nix.extend {
inherit pkgs;
bubblewrapPackage = pkgs.writeShellApplication {
name = "my-bubblewrap";
@ -284,94 +284,6 @@ 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
@ -429,7 +341,7 @@ spec = parallel $ inTestM $ do
it "clears all set permissions - including ones set in base permissions" $ do
[i|
let
jail' = jail-nix.lib.extend {
jail' = jail-nix.extend {
inherit pkgs;
basePermissions = c: [
c.base

View file

@ -12,8 +12,8 @@ spec = parallel $ inTestM $ do
it "can be imported using callPackage" $ do
[i|
let
jail-nix-without-flake = import <jail-nix> {};
jail-without-flake = jail-nix-without-flake.lib.init pkgs;
jail-nix-without-flake = pkgs.callPackage <jail-nix> {};
jail-without-flake = jail-nix-without-flake.init pkgs;
in
jail-without-flake "hello" pkgs.hello []
|]
@ -85,7 +85,7 @@ spec = parallel $ inTestM $ do
liftIO $ writeFile secretPath "hunter2"
[i|
let
jail' = jail-nix.lib.extend {
jail' = jail-nix.extend {
inherit pkgs;
basePermissions = c: [
(c.readonly #{toNixString secretPath})
@ -101,7 +101,7 @@ spec = parallel $ inTestM $ do
it "allows overriding the bubblewrap package" $ do
[i|
let
jail' = jail-nix.lib.extend {
jail' = jail-nix.extend {
inherit pkgs;
bubblewrapPackage = pkgs.writeShellApplication {
name = "my-bubblewrap";

View file

@ -16,7 +16,7 @@ spec = parallel $ inTestM $ do
let
pkgs = import <nixpkgs> { overlays = [ jailOverlay ]; };
jailOverlay = final: prev:
jail-nix.lib.mkOverlay {
jail-nix.mkOverlay {
inherit final prev;
packages = combinators: with combinators; {
nodejs = [ (readonly #{toNixString $ tmpDir </> "visible"}) ];
@ -53,7 +53,7 @@ spec = parallel $ inTestM $ do
let
pkgs = import <nixpkgs> { overlays = [ jailOverlay ]; };
jailOverlay = final: prev:
jail-nix.lib.mkOverlay {
jail-nix.mkOverlay {
inherit final prev;
additionalCombinators = combinators: {
my-combinator = combinators.readonly #{toNixString $ tmpDir </> "file"};
@ -76,7 +76,7 @@ spec = parallel $ inTestM $ do
let
pkgs = import <nixpkgs> { overlays = [ jailOverlay ]; };
jailOverlay = final: prev:
jail-nix.lib.mkOverlay {
jail-nix.mkOverlay {
inherit final prev;
basePermissions = combinators: [
(combinators.readonly #{toNixString $ tmpDir </> "file"})
@ -100,7 +100,7 @@ spec = parallel $ inTestM $ do
let
pkgs = import <nixpkgs> { overlays = [ jailOverlay ]; };
jailOverlay = final: prev:
jail-nix.lib.mkOverlay {
jail-nix.mkOverlay {
inherit final prev;
bubblewrapPackage = pkgs.writeShellApplication {
name = "my-bubblewrap";

View file

@ -158,7 +158,7 @@ runNixDrvWithinVm vmConfig nixDrvExpr = do
};
script = let
jail-nix = import <jail-nix> {};
jail = jail-nix.lib.extend { inherit pkgs; suppressExperimentalWarnings = true; };
jail = jail-nix.extend { inherit pkgs; };
testToRun = #{nixDrvExpr};
in ''
(
@ -204,7 +204,7 @@ evalNixExpr nixExpr = do
lib = pkgs.lib;
sh = script: (pkgs.writeShellApplication { name = "test-script"; text = script; });
jail-nix = import <jail-nix> {};
jail = jail-nix.lib.extend { inherit pkgs; suppressExperimentalWarnings = true; };
jail = jail-nix.extend { inherit pkgs; };
in #{nixExpr}
|]
liftIO $

8
website/flake.lock generated
View file

@ -2,16 +2,16 @@
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1783389287,
"narHash": "sha256-0xIy4dVLqq47rA+mRy0hXDfjhQd4E5PoIns/RmB7nR4=",
"lastModified": 1769318308,
"narHash": "sha256-Mjx6p96Pkefks3+aA+72lu1xVehb6mv2yTUUqmSet6Q=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "0ad6f47ea4fe188f4bc8f0380f93ae8523337c6c",
"rev": "1cd347bf3355fce6c64ab37d3967b4a2cb4b878c",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-26.05",
"ref": "nixos-25.11",
"repo": "nixpkgs",
"type": "github"
}

View file

@ -1,7 +1,7 @@
{
# This is a separate flake file from the one at the root because we don't
# want to depend on nixpkgs
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-26.05";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
outputs =
{ self, nixpkgs, ... }:
@ -10,7 +10,7 @@
lib = pkgs.lib;
repo = rec {
rev = if self ? rev then self.rev else lib.warn "Repo is dirty, links point to main!" "main";
rev = if self ? rev then self.rev else lib.warn "Repo is dirty, links will not work!" "???";
shortRev = lib.substring 0 8 rev;
urls = {
base = "https://git.sr.ht/~alexdavid/jail.nix";
@ -34,16 +34,9 @@
in
''
### ${name}
<a class="source" href="${repo.urls.file file attrPos.line}">Source</a>
**${name} :: ${combinatorObj.sig}**
${
if combinatorObj ? aliases then
"Aliases: ${lib.concatMapStringsSep ", " (alias: "`${alias}`") combinatorObj.aliases}"
else
""
}
[Source](${repo.urls.file file attrPos.line})
${combinatorObj.doc}
'';
@ -51,9 +44,7 @@
combinatorDocs =
let
allCombinators =
(import (docsFileSet + /lib/combinators.nix) { inherit pkgs; } (
throw "Docs must not depend on jail arg"
)).docs;
(import (docsFileSet + /lib/combinators.nix) pkgs (throw "Docs must not depend on jail arg")).docs;
formatSection =
filter:
@ -84,7 +75,7 @@
${hr}
${formatSection (v: !(is "includedInBasePermissions" v || is "internal" v || is "experimental" v))}
${formatSection (v: !(is "deprecated" v || is "includedInBasePermissions" v || is "internal" v))}
${hr}
@ -98,17 +89,12 @@
${hr}
## Experimental Combinators
## Deprecated Combinators
The following combinators are experimental, and may be removed, or
have breaking changes in the future. Please reach out if you have any
feedback on them.
The following combinators have been deprecated, and may be removed in the
future.
Using these combinators will emit an evaluation warning, you can
suppress this warning with
[suppressExperimentalWarnings](advanced-configuration.md#suppressexperimentalwarnings).
${formatSection (is "experimental")}
${formatSection (is "deprecated")}
'';
mkdocsSettings = {
@ -118,7 +104,6 @@
name = "readthedocs";
hljs_languages = [ "nix" ];
};
extra_css = [ "extra.css" ];
# Copyright is placed directly into the page within a <p> tag.
# Concatenating with </p><p> here is super janky, but good enough for
# now.

View file

@ -76,9 +76,3 @@ jail = jail-nix.lib.extend {
This configuration option allows you to override what bubblewrap package
jail.nix uses for its jails. The default value is `pkgs.bubblewrap`.
## suppressExperimentalWarnings
By default, jail.nix will emit an evaluation warning if you use an
[experimental combinator](combinators.md#experimental-combinators). Setting
this to true suppresses these warnings.

View file

@ -1,9 +0,0 @@
.document {
position: relative;
}
a.source {
position: absolute;
margin-top: -48px;
right: 0;
}

View file

@ -17,17 +17,17 @@ To install jail.nix using flakes, add jail.nix to your flake inputs:
}
```
### Non-flakes
### callPackage
To install jail.nix using a different dependency manager like
[niv](https://github.com/nmattia/niv), you can use the following snippet:
[niv](https://github.com/nmattia/niv), you can use `pkgs.callPackage`:
```nix
let
sources = import ./nix/sources.nix;
pkgs = import sources.nixpkgs {};
jail-nix = import sources."jail.nix" {};
jail = jail-nix.lib.init pkgs;
jail-nix = pkgs.callPackage sources."jail.nix" {};
jail = jail-nix.init pkgs;
in
# ...
```
@ -105,8 +105,6 @@ optionally be passed into `mkOverlay` attribute set as well.
Example:
```nix
sources = import ./nix/sources.nix;
jail-nix = import sources."jail.nix" { };
jailOverlay = final: prev: jail-nix.lib.mkOverlay {
inherit final prev;
packages = combinators: with combinators; {
@ -114,7 +112,7 @@ jailOverlay = final: prev: jail-nix.lib.mkOverlay {
firefox = [ network gui gpu ];
};
};
pkgs = import sources.nixpkgs {
pkgs = import nixpkgs {
system = "x86_64-linux";
overlays = [ jailOverlay ];
};