diff --git a/lib/combinators/network.nix b/lib/combinators/network.nix index 603cc27..a05154a 100644 --- a/lib/combinators/network.nix +++ b/lib/combinators/network.nix @@ -7,6 +7,7 @@ let runtime-deep-ro-bind share-ns time-zone + try-readonly unsafe-add-raw-args write-text ; @@ -30,6 +31,7 @@ in (runtime-deep-ro-bind "/etc/nsswitch.conf") (runtime-deep-ro-bind "/etc/resolv.conf") (runtime-deep-ro-bind "/etc/ssl") + (try-readonly "/run/systemd/resolve") (write-text "/etc/hostname" "${state.hostname}\n") (unsafe-add-raw-args "--hostname ${escape state.hostname}") ] state diff --git a/tests/spec/CombinatorsSpec.hs b/tests/spec/CombinatorsSpec.hs index 9eb47e2..87bf5c4 100644 --- a/tests/spec/CombinatorsSpec.hs +++ b/tests/spec/CombinatorsSpec.hs @@ -5,12 +5,12 @@ import Control.Monad import Data.List.Extra import Data.Maybe (fromJust) import Data.String.Interpolate.Util +import GHC.IO.Exception (ExitCode (..)) import System.Directory.Extra (createDirectoryIfMissing, doesFileExist, doesPathExist) import System.Posix (getProcessStatus, sigKILL, signalProcess) import System.Process qualified as Process import Test.Hspec import TestPrelude -import GHC.IO.Exception (ExitCode(..)) spec :: Spec spec = parallel $ inTestM $ do @@ -115,6 +115,15 @@ spec = parallel $ inTestM $ do |] liftIO $ out `shouldContain` "

Example Domain

" + it "allows overriding the hostname with set-hostname" $ do + [i| + jail "test" (sh ''${pkgs.nettools}/bin/hostname'') (c: [ + (c.set-hostname "my-hostname") + c.network + ]) + |] + `shouldOutput` "my-hostname\n" + it "does not add too many bwrap args" $ do out <- runNixDrv @@ -137,6 +146,18 @@ spec = parallel $ inTestM $ do |] liftIO $ (read out :: Int) `shouldSatisfy` (< 100) + it "works on systems using systemd-resolved" $ do + out <- + runNixDrvWithinVm + [i| { services.resolved.enable = true; } |] + [i| + jail "test" (sh ''curl https://example.org'') (c: [ + (c.add-pkg-deps [ pkgs.curl ]) + c.network + ]) + |] + liftIO $ out `shouldContain` "

Example Domain

" + describe "no-die-with-parent" $ do let waitForChildPid :: Process.Pid -> TestM Process.Pid waitForChildPid pid = do diff --git a/tests/spec/TestPrelude.hs b/tests/spec/TestPrelude.hs index a12c225..d96e0e7 100644 --- a/tests/spec/TestPrelude.hs +++ b/tests/spec/TestPrelude.hs @@ -11,6 +11,7 @@ module TestPrelude liftIO, runNixDrv, runNixDrvWithExitCode, + runNixDrvWithinVm, spawnNixDrv, toNixString, void, @@ -130,6 +131,55 @@ runNixDrvWithExitCode = liftIO . flip Process.readCreateProcessWithExitCode "" < spawnNixDrv :: String -> TestM (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) spawnNixDrv = liftIO . createProcess <=< mkNixDrvProccess +runNixDrvWithinVm :: String -> String -> TestM String +runNixDrvWithinVm vmConfig nixDrvExpr = do + testDir <- getTestDir + testOutputFilePath <- ( "test-output") <$> getTestDir + liftIO $ writeFile testOutputFilePath "---\n" + void $ + runNixDrvWithExitCode + [i| + let + system = import { + modules = [ + ({ pkgs, lib, ... }: { + system.stateVersion = "25.05"; + virtualisation.vmVariant.virtualisation = { + graphics = false; + sharedDirectories.testOutput = { source = #{toNixString testDir}; target = #{toNixString testDir}; }; + + }; + systemd.services.run-jail-nix-vm-test = { + wantedBy = [ "multi-user.target" ]; + after = [ "network-online.target" ]; + wants = [ "network-online.target" ]; + environment = { + HOME = "/root"; + }; + script = let + jail-nix = import {}; + jail = jail-nix.extend { inherit pkgs; }; + testToRun = #{nixDrvExpr}; + in '' + ( + ${lib.getExe testToRun} || echo 'Test failed' + ) >${#{toNixString testOutputFilePath}} 2>&1 + shutdown now + ''; + serviceConfig.type = "oneshot"; + }; + }) + #{vmConfig} + ]; + }; + in sh '' + TMP_DIR=$(${pkgs.coreutils}/bin/mktemp -d) + NIX_DISK_IMAGE="$TMP_DIR/img" ${system.config.system.build.vm}/bin/run-nixos-vm + ${pkgs.coreutils}/bin/rm -rf "$TMP_DIR" + '' + |] + liftIO $ readFile testOutputFilePath + buildNixDrvPath :: FilePath -> TestM FilePath buildNixDrvPath drvPath = do liftIO $