Flake and parametrized derivations

I’d like to write a flake that exports “parametrized” packages i.e. derivations like

website = { api_path ? "example.com" }: mkDerivation { postPatch = "code to replace api url in js code" }; 

How should I export this in my flake? I guess packages are only fon non-parametrized packages. I plan to add a nixos module but I also would like to provide an option for non-nixos users.

1 Like

You can make your package overridable, so that users of if can use website.override { api_path = "..."; }.

Define your package using callPackage, e.g.

# flake.nix
website = callPackage ./package.nix {};

# package.nix
{ stdenv
, api_path ? "example.com"
}:

stdenv.mkDerivation {
  ...
}

this will make the website.override function available (alternatively you may use lib.makeOverridable directly instead of callPackage).

4 Likes

Oh I see elegant solution, thanks! Also is there a way to put generic nix helper functions not related to packages available to users of the flake? I guess I can put it in a randon attribute of the output but there are maybe some conventions?

1 Like

The convention is to have arbitrary functions in lib.

1 Like