From f55483fb3854f684e6e90e382520d964a3baba04 Mon Sep 17 00:00:00 2001 From: Alex David Date: Mon, 13 Oct 2025 16:06:29 -0700 Subject: [PATCH] Forward `override` to jailed derivations On Nixos, it may be desirable to install some software using `programs.*.enable = true`. Jail.nix mostly supports this because you can either use `jail-nix.mkOverlay`, or just use Nixos `programs.*.package = jail ...` to inject a jailed version of the package. However, for some modules, like Firefox, the Nixos module calls `override` on the package. Normally, you would just call `override` on the package passed into jail, but this is not possible to do within a Nixos module. To address this, this patch will check to see if a package passed into jail has an `override` field, and if so will add an `override` on the jailed derivation that passes the override function to the entrypoint. Thanks to ~juanjakobo for reporting this! Fixes https://todo.sr.ht/~alexdavid/jail.nix/1 --- lib/jail.nix | 14 ++++++++++++-- tests/spec/JailSpec.hs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/lib/jail.nix b/lib/jail.nix index 313ecd2..cca88da 100644 --- a/lib/jail.nix +++ b/lib/jail.nix @@ -111,8 +111,18 @@ let } ) - # forward man pages - (jailed: if exe ? man then jailed // { inherit (exe) man; } else jailed) + # Add additional properties on the jailed derivation + ( + jailed: + jailed + # forward man pages + // lib.optionalAttrs (exe ? man) { inherit (exe) man; } + + # forward `override` + // lib.optionalAttrs (exe ? override) { + override = overrideFn: jail name (exe.override overrideFn) permissions; + } + ) ] ); diff --git a/tests/spec/JailSpec.hs b/tests/spec/JailSpec.hs index de88545..1427e1c 100644 --- a/tests/spec/JailSpec.hs +++ b/tests/spec/JailSpec.hs @@ -29,6 +29,46 @@ spec = parallel $ inTestM $ do [i| jail "test" (sh ''echo -n "''${MY_ENV_VAR-unset}"'') [] |] `shouldOutput` "unset" + describe "forwarding package overrides to the jailed derivation" $ do + -- This is a nix expression of a package that prints out the value of an + -- overridable `run`: + let overrideablePackage = + [i| pkgs.callPackage (opts: sh opts.run) { run = "printf unchanged"; } |] + + it "successfully wraps overridable packages" $ do + [i| + let some-pkg = #{overrideablePackage}; in + jail "some-pkg" some-pkg [] + |] + `shouldOutput` "unchanged" + + it "forwards overrides to the underlying package" $ do + [i| + let + some-pkg = #{overrideablePackage}; + jailed-some-pkg = jail "some-pkg" some-pkg []; + in + jailed-some-pkg.override { run = "printf 'new message!'"; } + |] + `shouldOutput` "new message!" + + it "correctly sets the passed combinators on the overridden package" $ do + [i| + let + some-pkg = #{overrideablePackage}; + jailed-some-pkg = jail "some-pkg" some-pkg (c: [ + (c.write-text "/foo" "foo contents") + ]); + in + jailed-some-pkg.override { run = "cat /foo"; } + |] + `shouldOutput` "foo contents" + + it "does not set `override` if the jailed package did not have it" $ do + jailHasOverride <- + evalNixExpr [i| (jail "not-overridable" (sh "") []) ? override |] + liftIO $ jailHasOverride `shouldBe` False + describe "advanced configuration" $ do it "allows overriding base permissions shared across all jails" $ do tmpDir <- getTestDir