1 # Check that overriding works for trivial-builders like
2 # `writeShellScript` via `overrideAttrs`. This is useful
3 # to override the `checkPhase`, e. g. if you want
4 # to disable extglob in `writeShellScript`.
6 # Run using `nix-build -A tests.trivial-builders.overriding`.
21 echo @(success|failure)
26 writeShellScript "test-trivial-overriding-${case}" extglobScript;
28 callPackageCase = case: callPackage (
30 writeShellScript "test-trivial-callpackage-overriding-${case}" extglobScript
34 writeShellScriptBin "test-trivial-overriding-bin-${case}" extglobScript;
36 # building this derivation would fail without overriding
37 textFileCase = writeTextFile {
38 name = "test-trivial-overriding-text-file";
47 disallowExtglob = x: x.overrideAttrs (_: {
49 ${stdenv.shell} -n "$target"
53 # Run old checkPhase, but only succeed if it fails.
54 # This HACK is required because we can't introspect build failures
55 # in nix: With `assertFail` we want to make sure that the default
56 # `checkPhase` would fail if extglob was used in the script.
57 assertFail = x: x.overrideAttrs (old: {
65 mkCase = case: outcome: isBin:
67 drv = lib.pipe outcome ([ case ] ++ lib.optionals (outcome == "fail") [ disallowExtglob assertFail ]);
68 in if isBin then "${drv}/bin/${drv.name}" else drv;
70 writeTextOverrides = {
71 # Make sure extglob works by default
72 simpleSucc = mkCase simpleCase "succ" false;
73 # Ensure it's possible to fail; in this case extglob is not enabled
74 simpleFail = mkCase simpleCase "fail" false;
75 # Do the same checks after wrapping with callPackage
76 # to make sure callPackage doesn't mess with the override
77 callpSucc = mkCase callPackageCase "succ" false;
78 callpFail = mkCase callPackageCase "fail" false;
79 # Do the same check using `writeShellScriptBin`
80 binSucc = mkCase binCase "succ" true;
81 binFail = mkCase binCase "fail" true;
82 # Check that we can also override plain writeTextFile
83 textFileSuccess = textFileCase.overrideAttrs (_: {
88 # `runTest` forces nix to build the script of our test case and
89 # run its `checkPhase` which is our main interest. Additionally
90 # it executes the script and thus makes sure that extglob also
94 name = script.name or (builtins.baseNameOf script);
95 in writeShellScript "run-${name}" ''
96 if [ "$(${script})" != "success" ]; then
97 echo "Failed in ${name}"
103 runCommand "test-writeShellScript-overriding" {
104 passthru = { inherit writeTextOverrides; };
106 ${lib.concatMapStrings (test: ''
108 '') (lib.attrValues writeTextOverrides)}