How to use pkg.writeText

I am unable to use pkg.writeText to create a config file during the install phase and receive the following error:

...
installing
/nix/store/ciccv6r75031fam9d50lijmijsxc2yx9-stdenv-linux/setup: line 1341: /nix/store/w66220n6sw35a9k1wf6gni4l06w9jypa--out-test: Permission denied
builder for '/nix/store/h1dn9phv4mh88rxis0yr8hdnz4l3iidk-testpoc.drv' failed with exit code 126

I wrote the following simple derivation to illustrate:

{ stdenv, lib, pkgs, fetchFromGitHub }:

with lib;

let
in
  stdenv.mkDerivation rec {
    name = "testpoc";

    src = fetchFromGitHub {
      owner = "nvchad";
      repo = "nvchad";
      rev  = "08a16b9201c13eb244b14dcc8e1e078a0ac76725";
      sha256 = "1kl2psvimq5vlhrn9isgbkpq140f9lmli937n9y87gc3017pj8m8";
    };

    installPhase = ''
      ${pkgs.writeText "$out/test" ''
        hello world
      ''}
    '';
  }

Why does this not work?

1 Like

Think about what you are trying to do. The function pkgs.writeText creates a derivation for a simple text file in the Nix store. When interpolated into a string, this derivation evaluates to the path of the file in the Nix store. Your builder is trying to run the command /nix/store/hklgc4iljr0p28z9g6xn7mif5w8zkwk4--out-test or something, which errors. I think the confusion stems from the fact that the first argument to pkgs.writeText is just the name of the file in the Nix store.

Your desired result could be achieved by something like this:

  installPhase = ''
      mkdir $out
      cp ${pkgs.writeText "$out/test" ''
        hello world
      ''} $out/test
    '';

It might be more idiomatic to write something like this:

    test = pkgs.writeText "test" ''
      hello world!
    '';

    installPhase = ''
      mkdir $out
      cp $test $out/test
    '';
2 Likes

thanks @wenngle for a clear answer and the path vs the return value clears that up for me however it’s till not working

I’ve currently got:

{ stdenv, pkgs, fetchFromGitHub }:

stdenv.mkDerivation rec {
  name = "testpoc";

  src = fetchFromGitHub {
    owner = "nvchad";
    repo = "nvchad";
    rev  = "08a16b9201c13eb244b14dcc8e1e078a0ac76725";
    sha256 = "1kl2psvimq5vlhrn9isgbkpq140f9lmli937n9y87gc3017pj8m8";
  };

  test = pkgs.writeText "test" ''
    hello world
  '';

  installPhase = ''
    #cp $test $out/test
  '';
}

building Nix…
building the system configuration…
error: cannot coerce a function to a string, at /nix/var/nix/profiles/per-user/root/channels/nixos/pkgs/stdenv/generic/make-derivation.nix:205:7

I’m unable to replicate your result. Here’s exactly what I’m working with:

  • I created a file, default.nix, in a new directory with the following contents:
let pkgs = import <nixpkgs> {}; in
pkgs.callPackage ({ stdenv, pkgs, fetchFromGitHub }:

stdenv.mkDerivation rec {
  name = "testpoc";

  src = fetchFromGitHub {
    owner = "nvchad";
    repo = "nvchad";
    rev  = "08a16b9201c13eb244b14dcc8e1e078a0ac76725";
    sha256 = "1kl2psvimq5vlhrn9isgbkpq140f9lmli937n9y87gc3017pj8m8";
  };

  test = pkgs.writeText "test" ''
    hello world
  '';

  installPhase = ''
    mkdir $out
    cp $test $out/test
  '';
}) {}
  • I built it with nix-build
  • The command executed successfully and created a result symlink in my current directory.
$ cat result/test
hello world
1 Like

awesome got it working thanks @wenngle :slight_smile: