Add file to store with specific name

Hi. I’m trying to configure HAProxy in NixOS, using something like this:

{ lib, config, pkgs, nixpkgs-unstable, ... }:
{
  imports = [ ../oauth2-proxy/default.nix ];

  services.haproxy = {
    package = pkgs.unstable.haproxy;
    enable = true;
    config = lib.concatLines [
      ''
      global
        lua-load ${./haproxy-lua-http.lua}
        lua-load ${./auth-request.lua}

      ''
      (builtins.readFile ./haproxy.cfg)
    ];
  };

  environment.systemPackages = with pkgs; [
    (lua.withPackages(ps: with ps; [ cjson ]))
  ];

  users.groups.certs.members = [ "haproxy" ];
}

auth-request.lua has the line

local http = require("haproxy-lua-http")

and thus it requires a file named precisely “haproxy-lua-http.lua” to be loaded. When that file is added to the Nix store, however, the filename is changed so a hash can be added to it. Is there some way to circumvent this, for example by convincing Nix to make the path “/nix/store/asd1234-something/haproxy-lua-http.lua” instead of “/nix/store/asd1234-haproxy-lua-http.lua”? Since the documentation is so bad, I was not able to find anything on this.

I do not want to somehow patch the correct path into auth-request.lua, as this would probably be very complicated to do as well, especially since there is so little documentation on how I would do this. But maybe there is a very simple way to do this and it could be the better solution.

Have a look at the trivial builders section of the Nixpkgs manual. writeTextFile can do this easily if you can read the text of the file you’re adding (set the destination attribute to the desired file name); you could also use runCommand to create any sort of arbitrary directory structure you want, including symlinks.

1 Like

Thank you! I ended up simply solving it by putting all the files in a directory called lua and then using that path (since my HAProxy config needed a path like that anyway), but your solution definitely seems the most flexible in the case that that would not have been sufficient.

1 Like