builtins.readFile can't read from third party derivation

I tried following flake.nix with nix (Nix) 2.24.10

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };

  outputs =
    { self, nixpkgs }:
    let
      system = "x86_64-linux";
      pkgs = nixpkgs.legacyPackages.${system};
    in
    {
      packages.${system} = {
        default = let
          config = builtins.readFile "${pkgs.sway}/etc/sway/config" + "include ...";
        in
          builtins.toFile "sway_config" config;
      };
    };
}

and I got error:

error: access to absolute path '/nix/store/69qg78xfrpxfgxb4k5yvg3f856hf4j3h-sway-unwrapped-1.10' is forbidden in pure evaluation mode (use '--impure' to override)

However, via my derivation, build was successful.

diff --git a/flake.nix b/flake.nix
index 0d45bb3..81d147f 100644
--- a/flake.nix
+++ b/flake.nix
@@ -12,7 +12,8 @@
     {
       packages.${system} = {
         default = let
-          config = builtins.readFile "${pkgs.sway}/etc/sway/config" + "include ...";
+          drv = pkgs.runCommandLocal "config" {} "cp ${pkgs.sway}/etc/sway/config $out";
+          config = builtins.readFile "${drv}" + "include ...";
         in
           builtins.toFile "sway_config" config;
       };

These are expected behavior?

Yes it’s expected (though I’m mildly surprised that store paths are included under the prohibition of absolute paths when using flakes).

But there’s no reason to readFile and toFile, just runCommand with a cp/install command.
Use echo "whatever" >>/cat <<eof >>to append to the file if needed.

1 Like

Certainly, can substitute to runCommand mostly. thanks.