summary refs log tree commit diff
path: root/pkgs/build-support/trivial-builders.nix
diff options
context:
space:
mode:
authordeliciouslytyped <47436522+deliciouslytyped@users.noreply.github.com>2021-06-14 15:06:23 +0200
committersterni <sternenseemann@systemli.org>2021-06-18 01:39:59 +0200
commita71e906e3a0bec9c5fece94262e96de83e58c1f3 (patch)
tree0e8dd8f98769a0d32ca3704cb9983acc389d255d /pkgs/build-support/trivial-builders.nix
parent204eb98e85679f3da6190048730434c3e62bfb90 (diff)
downloadnixpkgs-a71e906e3a0bec9c5fece94262e96de83e58c1f3.tar
nixpkgs-a71e906e3a0bec9c5fece94262e96de83e58c1f3.tar.gz
nixpkgs-a71e906e3a0bec9c5fece94262e96de83e58c1f3.tar.bz2
nixpkgs-a71e906e3a0bec9c5fece94262e96de83e58c1f3.tar.lz
nixpkgs-a71e906e3a0bec9c5fece94262e96de83e58c1f3.tar.xz
nixpkgs-a71e906e3a0bec9c5fece94262e96de83e58c1f3.tar.zst
nixpkgs-a71e906e3a0bec9c5fece94262e96de83e58c1f3.zip
trivial-builders: refactor writeTextFile to be overridable
This fixes #126344, specifically with the goal of enabling overriding the
checkPhase argument. See `design notes` at the end for details.

This allows among other things, enabling bash extension for the `checkPhase`.
Previously using such bash extensions was prohibited by the `writeShellScript`
code because there was no way to enable the extension in the checker.

As an example:

```nix
(writeShellScript "foo" ''
  shopt -s extglob
  echo @(foo|bar)
'').overrideAttrs (old: {
  checkPhase = ''
    # use subshell to preserve outer environment
    (
      export BASHOPTS
      shopt -s extglob
      ${old.checkPhase}
    )
  '';
})
```

This commit also adds tests for this feature to `pkgs/tests/default.nix`,
under `trivial-overriding`. The test code is located at
`pkgs/build-support/trivial-builders/test-overriding.nix`.

Design notes:
-------------

Per discussion with @sternenseemann, the original approach of just wrapping
`writeTextFile` in `makeOverridable` had the issue that combined with `callPackage`
in the following form, would shadow the `.override` attribute of the `writeTextFile`:

```nix
with import <nixpkgs>;
callPackage ({writeShellScript}: writeShellScript "foo" "echo foo")
```

A better approach can be seen in this commit, where `checkPhase` is moved
from an argument of `writeTextFile`, which is substituted into `buildCommand`,
into an `mkDerivation` argument, which is substituted from the environment
and `eval`-ed. (see the source)

This way we can simple use `.overideAttrs` as usual, and this also makes
`checkPhase` a bit more conformant to `mkDerivation` naming, with respect to
phases generally being overridable attrs.

Co-authored-by: sterni <sternenseemann@systemli.org>
Co-authored-by: Naïm Favier <n@monade.li>
Diffstat (limited to 'pkgs/build-support/trivial-builders.nix')
-rw-r--r--pkgs/build-support/trivial-builders.nix4
1 files changed, 2 insertions, 2 deletions
diff --git a/pkgs/build-support/trivial-builders.nix b/pkgs/build-support/trivial-builders.nix
index 219f808403c..6f51ba512c1 100644
--- a/pkgs/build-support/trivial-builders.nix
+++ b/pkgs/build-support/trivial-builders.nix
@@ -116,7 +116,7 @@ rec {
     , checkPhase ? ""    # syntax checks, e.g. for scripts
     }:
     runCommand name
-      { inherit text executable;
+      { inherit text executable checkPhase;
         passAsFile = [ "text" ];
         # Pointless to do this on a remote machine.
         preferLocalBuild = true;
@@ -132,7 +132,7 @@ rec {
           echo -n "$text" > "$n"
         fi
 
-        ${checkPhase}
+        eval "$checkPhase"
 
         (test -n "$executable" && chmod +x "$n") || true
       '';