Reference to a flake gives the source instead of the output

I’m setting up Nginx to serve the built output of a flake. However, Nginx seems to be trying to read from the source of the flake, instead of the build output.

Nginx log output:

"/nix/store/snsbckbsx276fr2fjpil097r03vyikhw-source/bin/my-site-public/index.html" is not found
Note the source in the path -----------------^

Here is the Nginx config:

  services.nginx = {
    enable = true;
    virtualHosts."localhost" = {
      default = true;
      # my-site is a input for the surrounding nixosConfiguration flake
      root = "${my-site}/bin/my-site-public";
    };
  };

This is pulled into the modules of my nixosConfigurations, providing my-site as an input. Here is the my-site flake:

{
  description = "Flake for my site";
  outputs = { self, nixpkgs }:
    let
      pkgs = import nixpkgs { system = "x86_64-linux"; };
      hugo = pkgs.hugo;
      stdenv = pkgs.stdenv;
    in {
      packages.x86_64-linux.default = stdenv.mkDerivation {
        name = "my-site";
        src = self;
        buildPhase = "${hugo}/bin/hugo";
        installPhase = ''
          mkdir -p $out/bin;
          # Note this is the path I would expect Nginx to be given
          cp -r public/ $out/bin/my-site-public;
        '';
      };
  };
}

In fact when I check my Nix store, the my-site flake hasn’t even been built. Only the source is present. How do I indicate that I want the path to the output of the flake?

Thinking it through some more I figured out the issue. I was getting the outPath from the flake itself, but what I really wanted was the outPath from the derivation inside it.

There’s probably a neater way to do this but pointing to the derivation fixed the issue.

  services.nginx = {
    enable = true;
    virtualHosts."localhost" = {
      default = true;
      # my-site is a input for the surrounding nixosConfiguration flake
      root = "${my-site.packages.x86_64-linux.default}/bin/my-site-public";
    };
  };
1 Like