Is there a way to get the absolute path of a derivation output?

I have a derivation whose output is a bunch of files. Currently, I am symlinking the derivations output to a known location, then using that location for what I actually want to do with the files. That works, but I am just wondering if there’s a way to skip this step.

Here’s what I’m currently doing (just for clarification):

{ config, pkgs, ... }:
let
  config_files = pkgs.stdenv.mkDerivation {
    name = "surfshark-config";
    src = pkgs.fetchurl {
      url = "https://my.surfshark.com/vpn/api/v1/server/configurations";
      sha256 = "sha256-QY/kRqJK5yyTarcO7YhHhUm89gMSUzq7d+Uv0d1kxKM=";
    };
    phases = [ "installPhase" ];
    buildInputs = [ pkgs.unzip pkgs.rename ];
    installPhase = ''
      unzip $src 
      find . -type f ! -name '*_udp.ovpn' -delete
      find . -type f -exec sed -i "s+auth-user-pass+auth-user-pass \"${config.sops.secrets.openvpn.path}\"+" {} +
      rename 's/prod.surfshark.com_udp.//' *
      mkdir -p $out
      mv * $out
    '';
  };

  getConfig = filePath: {
    name = "${builtins.substring 0 (builtins.stringLength filePath - 5) filePath}";
    value = { config = '' config /etc/openvpn/client/configs/${filePath} ''; autoStart = false; };
  };
  openVPNConfigs = map getConfig (builtins.attrNames (builtins.readDir config_files));
in
{
  sops.secrets.openvpn = { };
  networking.networkmanager.plugins = [ pkgs.networkmanager-openvpn ];

  environment.etc."openvpn/client/configs".source = config_files;
  services.openvpn.servers = builtins.listToAttrs openVPNConfigs;
}

Essentially, I would love to get rid of this line:

environment.etc."openvpn/client/configs".source = config_files;

Just use e.g. ''${configFiles}''. When you interpolate derivation into a string, it will automatically turn into its nix store path, e.g.:

$ nix repl
Welcome to Nix 2.18.1. Type :? for help.

nix-repl> :l <nixpkgs>
Added 19837 variables.

nix-repl> pkgs.hello
«derivation /nix/store/c7q0461my21f1zmx8jz98k3lkb0r6pr4-hello-2.12.1.drv»

nix-repl> ''hello is at ${pkgs.hello}''
"hello is at /nix/store/d29xassiyyf71ls2vbvx3vywrdcx52c3-hello-2.12.1"
1 Like

Oh, nice to know! And yes, worked like a charm. Thank you!