Best way to get files from non-binary package?

I have an overriden WordPress package, and I need to get the path to copy it into a local directory. I found that I can get the path with echo $buildInputs | tr " " "\n" | grep wordpress, but it seems like there would be a better and less fragile way to do this. I tried using a writeScript in default.nix, but I get a nix-shell error about not having multiple derivations.

My default.nix is:

let
  nixpkgs = import <nixpkgs> {};
  inherit (nixpkgs) stdenv fetchurl;
  sources = import ./nix/sources.nix { };
  pkgs = import sources.nixpkgs { };
  wordpress = pkgs.wordpress.overrideAttrs( oldAttrs: rec {
    version = "5.8.1";
    src = fetchurl {
      url = "https://wordpress.org/wordpress-${version}.tar.gz";
      sha256 = "90ca90c4afa37dadc8a4743b5cb111b20cda5f983ce073c2c0bebdce64fa822a";
    };
  });
in
pkgs.mkShell {
  buildInputs = [
    pkgs.mariadb
    pkgs.nginx
    pkgs.php
    pkgs.php74Packages.composer
    pkgs.wp-cli
    wordpress
  ];
}
1 Like

Are you trying to get the Nix store path of your custom wordpress derivation? Derivations have outPath attribute for that. When a derivation is coerced to a string (for example with builtins.toString <drv>) it becomes <drv>.outPath.

I think the easiest way the get the outputPath of your derivation would be adding shellHook attribute to you devShell like

pkgs.mkShell {
  buildInputs = [
    ...
  ];

  shellHook = ''
    echo "Nix store path of wordpress: ${wordpress}"
  '';
}

This way, when you enter the shell with nix-shell, it will print the Nix store path of the derivation. Or you could use nix-repl

nix-repl> :b wordpress.overrideAttrs( oldAttrs: rec {
    version = "5.8.1";
    src = fetchurl {
      url = "https://wordpress.org/wordpress-${version}.tar.gz";
      sha256 = "90ca90c4afa37dadc8a4743b5cb111b20cda5f983ce073c2c0bebdce64fa822a";
    };
  })

this derivation produced the following outputs:
  out -> /nix/store/blvbrdkmhan50h2dhrabr68x8xh9an1x-wordpress-5.8.1
2 Likes

Thank you for the shellHook tip! I decided on this:

  shellHook = ''
    export WORDPRESS_PATH="${wordpress}"
  '';