For what it’s worth, your original code didn’t work because the file you were importing didn’t exist within the build. Using cp ${./nginx.conf} $out/conf/nginx.conf
would have worked, since the nix expression ./nginx.conf
refers to a path next to the expression file, and nix will then track this dependency and determine that the build needs in, then going on to import the path into the nix store and substitute it as appropriate — if you make that change and add -v
to the cp
command, you’ll find that the actual postInstall
refers to a path like /nix/store/m3cbp1dgpn7jy70i1qhxr19si7p2ag8s-nginx.conf
.
That said, overriding nginx like this is not the most efficient way to do this, since every change to your config file will require rebuilding nginx from source. A better option might be to use a wrapper, like so:
{ pkgs ? import <nixpkgs> {} }:
with pkgs;
let
nginx-with-config = pkgs.writeScriptBin "nginx" ''
exec ${pkgs.nginx}/bin/nginx -c ${./nginx.conf} "$@"
'';
in mkShell {
name = "my-shell";
buildInputs = [
nginx-with-config
];
shellHook = ''
echo Let’s Nix!;
'';
}
Running nix-shell --run 'cat $(which nginx)'
will show you that the same import-into-store thing happens here.