Merge pull request #155892 from hercules-ci/nixos-etc-unit-test

nixos: Refactor to allow `etc` unit test

authored by Robert Hensing and committed by GitHub 6be11a84 22127ef5

+117 -6
+1 -1
nixos/modules/module-list.nix
··· 1156 1156 ./system/boot/systemd-nspawn.nix 1157 1157 ./system/boot/timesyncd.nix 1158 1158 ./system/boot/tmp.nix 1159 - ./system/etc/etc.nix 1159 + ./system/etc/etc-activation.nix 1160 1160 ./tasks/auto-upgrade.nix 1161 1161 ./tasks/bcache.nix 1162 1162 ./tasks/cpu-freq.nix
+12
nixos/modules/system/etc/etc-activation.nix
··· 1 + { config, lib, ... }: 2 + let 3 + inherit (lib) stringAfter; 4 + in { 5 + 6 + imports = [ ./etc.nix ]; 7 + 8 + config = { 9 + system.activationScripts.etc = 10 + stringAfter [ "users" "groups" ] config.system.build.etcActivationCommands; 11 + }; 12 + }
+3 -3
nixos/modules/system/etc/etc.nix
··· 66 66 67 67 { 68 68 69 + imports = [ ../build.nix ]; 70 + 69 71 ###### interface 70 72 71 73 options = { ··· 188 190 config = { 189 191 190 192 system.build.etc = etc; 191 - 192 - system.activationScripts.etc = stringAfter [ "users" "groups" ] 193 + system.build.etcActivationCommands = 193 194 '' 194 195 # Set up the statically computed bits of /etc. 195 196 echo "setting up /etc..." 196 197 ${pkgs.perl.withPackages (p: [ p.FileSlurp ])}/bin/perl ${./setup-etc.pl} ${etc}/etc 197 198 ''; 198 - 199 199 }; 200 200 201 201 }
+70
nixos/modules/system/etc/test.nix
··· 1 + { lib 2 + , coreutils 3 + , fakechroot 4 + , fakeroot 5 + , evalMinimalConfig 6 + , pkgsModule 7 + , runCommand 8 + , util-linux 9 + , vmTools 10 + , writeText 11 + }: 12 + let 13 + node = evalMinimalConfig ({ config, ... }: { 14 + imports = [ pkgsModule ../etc/etc.nix ]; 15 + environment.etc."passwd" = { 16 + text = passwdText; 17 + }; 18 + environment.etc."hosts" = { 19 + text = hostsText; 20 + mode = "0751"; 21 + }; 22 + }); 23 + passwdText = '' 24 + root:x:0:0:System administrator:/root:/run/current-system/sw/bin/bash 25 + ''; 26 + hostsText = '' 27 + 127.0.0.1 localhost 28 + ::1 localhost 29 + # testing... 30 + ''; 31 + in 32 + lib.recurseIntoAttrs { 33 + test-etc-vm = 34 + vmTools.runInLinuxVM (runCommand "test-etc-vm" { } '' 35 + mkdir -p /etc 36 + ${node.config.system.build.etcActivationCommands} 37 + set -x 38 + [[ -L /etc/passwd ]] 39 + diff /etc/passwd ${writeText "expected-passwd" passwdText} 40 + [[ 751 = $(stat --format %a /etc/hosts) ]] 41 + diff /etc/hosts ${writeText "expected-hosts" hostsText} 42 + set +x 43 + touch $out 44 + ''); 45 + 46 + # fakeroot is behaving weird 47 + test-etc-fakeroot = 48 + runCommand "test-etc" 49 + { 50 + nativeBuildInputs = [ 51 + fakeroot 52 + fakechroot 53 + # for chroot 54 + coreutils 55 + # fakechroot needs getopt, which is provided by util-linux 56 + util-linux 57 + ]; 58 + fakeRootCommands = '' 59 + mkdir -p /etc 60 + ${node.config.system.build.etcActivationCommands} 61 + diff /etc/hosts ${writeText "expected-hosts" hostsText} 62 + touch $out 63 + ''; 64 + } '' 65 + mkdir fake-root 66 + export FAKECHROOT_EXCLUDE_PATH=/dev:/proc:/sys:${builtins.storeDir}:$out 67 + fakechroot fakeroot chroot $PWD/fake-root bash -c 'source $stdenv/setup; eval "$fakeRootCommands"' 68 + ''; 69 + 70 + }
+1
nixos/tests/all-tests.nix
··· 141 141 env = handleTest ./env.nix {}; 142 142 ergo = handleTest ./ergo.nix {}; 143 143 ergochat = handleTest ./ergochat.nix {}; 144 + etc = pkgs.callPackage ../modules/system/etc/test.nix { inherit evalMinimalConfig; }; 144 145 etcd = handleTestOn ["x86_64-linux"] ./etcd.nix {}; 145 146 etcd-cluster = handleTestOn ["x86_64-linux"] ./etcd-cluster.nix {}; 146 147 etebase-server = handleTest ./etebase-server.nix {};
+8 -1
pkgs/tools/system/fakechroot/default.nix
··· 1 - { lib, stdenv, fetchFromGitHub, fetchpatch, autoreconfHook, perl }: 1 + { lib, stdenv, fetchFromGitHub, fetchpatch, autoreconfHook, nixosTests, perl }: 2 2 3 3 stdenv.mkDerivation rec { 4 4 pname = "fakechroot"; ··· 43 43 44 44 nativeBuildInputs = [ autoreconfHook ]; 45 45 buildInputs = [ perl ]; 46 + 47 + passthru = { 48 + tests = { 49 + # A lightweight *unit* test that exercises fakeroot and fakechroot together: 50 + nixos-etc = nixosTests.etc.test-etc-fakeroot; 51 + }; 52 + }; 46 53 47 54 meta = with lib; { 48 55 homepage = "https://github.com/dex4er/fakechroot";
+8 -1
pkgs/tools/system/fakeroot/default.nix
··· 1 - { lib, stdenv, fetchurl, fetchpatch, getopt, libcap, gnused }: 1 + { lib, stdenv, fetchurl, fetchpatch, getopt, libcap, gnused, nixosTests }: 2 2 3 3 stdenv.mkDerivation rec { 4 4 version = "1.23"; ··· 64 64 make wraptmpf.h 65 65 patch -p1 < ${patch-wraptmpf} 66 66 ''; 67 + 68 + passthru = { 69 + tests = { 70 + # A lightweight *unit* test that exercises fakeroot and fakechroot together: 71 + nixos-etc = nixosTests.etc.test-etc-fakeroot; 72 + }; 73 + }; 67 74 68 75 meta = { 69 76 homepage = "https://salsa.debian.org/clint/fakeroot";
+14
pkgs/top-level/all-packages.nix
··· 33466 33466 in 33467 33467 c.config.system.build // c; 33468 33468 33469 + /* 33470 + A NixOS/home-manager/arion/... module that sets the `pkgs` module argument. 33471 + */ 33472 + pkgsModule = { lib, options, ... }: { 33473 + config = 33474 + if options?nixpkgs.pkgs then { 33475 + # legacy / nixpkgs.nix style 33476 + nixpkgs.pkgs = pkgs; 33477 + } 33478 + else { 33479 + # minimal 33480 + _module.args.pkgs = pkgs; 33481 + }; 33482 + }; 33469 33483 33470 33484 /* 33471 33485 * Run a NixOS VM network test using this evaluation of Nixpkgs.