Using flake output package in existing service

I’m trying to build Caddy with the Tailscale plugin. After a bunch of flailing around I’ve settled on this approach Specifically I have this flake:

{
  inputs = {
    nixpkgs.url = "nixpkgs";
    flake-utils.url = "github:numtide/flake-utils";
    caddy.url = "github:vincentbernat/caddy-nix";
  };
  outputs =
    {
      self,
      nixpkgs,
      flake-utils,
      caddy,
    }:
    flake-utils.lib.eachDefaultSystem (
      system:
      let
        pkgs = import nixpkgs {
          inherit system;
          overlays = [ caddy.overlays.default ];
        };
      in
      {
        packages = {
          default = pkgs.caddy.withPlugins {
            plugins = [ "github.com/tailscale/caddy-tailscale@f21c01b660c896bdd6bacc37178dc00d9af282b4" ];
            hash = "sha256-zrL1wrWXbXnBrWHSnuNaoO2Q7R9GL3/DfUtS5vTqono=";
          };
        };
      }
    );
}

I can run nix build and get what looks like a working package. Now I want to use that from another module like this from roles/caddy.nix:

{
  config,
  system,
  ...
}:

{
  services.caddy = {
    enable = true;
    package = ...; # What here?
    email = "me";
  };
  age.secrets.tsAuthKey = {
    file = ../secrets/ts_auth_key.age;
    owner = config.services.caddy.user;
    group = config.services.caddy.group;
    mode = "600";
  };
  systemd.services.caddy.serviceConfig.EnvironmentFile = config.age.secrets.tsAuthKey.path;
}

How do I reference this package from my main NixOS flake? I have the following:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
    nixpkgsUnstable.url = "github:NixOS/nixpkgs/nixos-unstable";
    nixos-hardware.url = "github:NixOS/nixos-hardware/master";
    home-manager = {
      url = "github:nix-community/home-manager/release-24.11";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    agenix.url = "github:ryantm/agenix";
    simple-nixos-mailserver.url = "gitlab:simple-nixos-mailserver/nixos-mailserver/nixos-24.05";
    caddy.url = "path:pkgs/caddy";
  };

  outputs =
    {
      nixpkgs,
      nixpkgsUnstable,
      home-manager,
      nixos-hardware,
      agenix,
      simple-nixos-mailserver,
      caddy,
      ...
    }:
...

Then I have individual hosts with their own hosts/*/default.nix, one of which includes the above caddy.nix. But I’m not clear on how to reference the package, produced by the separate Caddy flake, in this one. I’ve tried something like package = caddy;, importing the flake path, passing it into the module as an argument, etc. Nothing seems to work.

So how do I build a package in a new flake, import it into my main server/infrastructure flake, then reference that built package in the default nixpkgs module definitions so it gets swapped out?

Thanks for any help. Happy to send along more config snippets if needed, I just don’t know what’s relevant and there’s a lot here. :slight_smile:

  1. Name the argset that’s passed to the outputs functon of the consumer flake. Usually people call it inputs but the name doesn’t matter as long as you’re consistent.
  outputs =
    inputs @ {
  1. Pass the binding into your config via the specialArgs arg of lib.nixosSystem:
nixosConfiguratios.FOO = lib.nixosSystem {
  specialArgs = { inherit inputs; }
  # ...
};
  1. Use that module arg in your config
{ pkgs, inputs, ... }:
{
  services.caddy.package = inputs.caddy.packages.${pkgs.stdenv.hostPlatform.system}.default;
}

I’m not 100% familiar with caddy or its module, but that’s usually what I do for other modules.