Fix network combinator with systemd resolved

This commit is contained in:
Alex David 2025-12-02 18:58:03 -08:00
commit eaf7e343d8
3 changed files with 74 additions and 1 deletions

View file

@ -7,6 +7,7 @@ let
runtime-deep-ro-bind runtime-deep-ro-bind
share-ns share-ns
time-zone time-zone
try-readonly
unsafe-add-raw-args unsafe-add-raw-args
write-text write-text
; ;
@ -30,6 +31,7 @@ in
(runtime-deep-ro-bind "/etc/nsswitch.conf") (runtime-deep-ro-bind "/etc/nsswitch.conf")
(runtime-deep-ro-bind "/etc/resolv.conf") (runtime-deep-ro-bind "/etc/resolv.conf")
(runtime-deep-ro-bind "/etc/ssl") (runtime-deep-ro-bind "/etc/ssl")
(try-readonly "/run/systemd/resolve")
(write-text "/etc/hostname" "${state.hostname}\n") (write-text "/etc/hostname" "${state.hostname}\n")
(unsafe-add-raw-args "--hostname ${escape state.hostname}") (unsafe-add-raw-args "--hostname ${escape state.hostname}")
] state ] state

View file

@ -5,12 +5,12 @@ import Control.Monad
import Data.List.Extra import Data.List.Extra
import Data.Maybe (fromJust) import Data.Maybe (fromJust)
import Data.String.Interpolate.Util import Data.String.Interpolate.Util
import GHC.IO.Exception (ExitCode (..))
import System.Directory.Extra (createDirectoryIfMissing, doesFileExist, doesPathExist) import System.Directory.Extra (createDirectoryIfMissing, doesFileExist, doesPathExist)
import System.Posix (getProcessStatus, sigKILL, signalProcess) import System.Posix (getProcessStatus, sigKILL, signalProcess)
import System.Process qualified as Process import System.Process qualified as Process
import Test.Hspec import Test.Hspec
import TestPrelude import TestPrelude
import GHC.IO.Exception (ExitCode(..))
spec :: Spec spec :: Spec
spec = parallel $ inTestM $ do spec = parallel $ inTestM $ do
@ -115,6 +115,15 @@ spec = parallel $ inTestM $ do
|] |]
liftIO $ out `shouldContain` "<h1>Example Domain</h1>" liftIO $ out `shouldContain` "<h1>Example Domain</h1>"
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 it "does not add too many bwrap args" $ do
out <- out <-
runNixDrv runNixDrv
@ -137,6 +146,18 @@ spec = parallel $ inTestM $ do
|] |]
liftIO $ (read out :: Int) `shouldSatisfy` (< 100) 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` "<h1>Example Domain</h1>"
describe "no-die-with-parent" $ do describe "no-die-with-parent" $ do
let waitForChildPid :: Process.Pid -> TestM Process.Pid let waitForChildPid :: Process.Pid -> TestM Process.Pid
waitForChildPid pid = do waitForChildPid pid = do

View file

@ -11,6 +11,7 @@ module TestPrelude
liftIO, liftIO,
runNixDrv, runNixDrv,
runNixDrvWithExitCode, runNixDrvWithExitCode,
runNixDrvWithinVm,
spawnNixDrv, spawnNixDrv,
toNixString, toNixString,
void, void,
@ -130,6 +131,55 @@ runNixDrvWithExitCode = liftIO . flip Process.readCreateProcessWithExitCode "" <
spawnNixDrv :: String -> TestM (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) spawnNixDrv :: String -> TestM (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
spawnNixDrv = liftIO . createProcess <=< mkNixDrvProccess 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 <nixpkgs/nixos/lib/eval-config.nix> {
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-nix> {};
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 :: FilePath -> TestM FilePath
buildNixDrvPath drvPath = do buildNixDrvPath drvPath = do
liftIO $ liftIO $