Generate configuration files using Nix.
Ready to dynamically generate configuration files in your flake-based setup? Nixago is a flake library for generating configuration files using Nix expressions as the data source.
Add Nixago as an input to your flake.nix
:
{
inputs = {
# ...
nixpkgs.url = "github:nixos/nixpkgs";
nixago.url = "github:jmgilman/nixago";
nixago.inputs.nixpkgs.follows = "nixpkgs";
# ...
};
}
Nixago offers various engines which can be used for transforming input data
into an output file. Nixago will default to the nix engine that utilizes
pkgs.formats
from nixpkgs.
let
data = {
"field1" = "value1";
"field2" = true;
};
in
nixago.lib.make {
inherit data;
output = "config.json";
format = "json"; # Optional if it matches the file extension
engine = nixago.engines.nix { }; # Optional as this is the default engine
}
The result of this invocation will be an attribute set with two attributes:
configFile
: A derivation for building the configuration file.shellHook
: A shell hook for managing the file.Building the derivation produces a file with the following output:
{
"field1": "value1",
"field2": true
}
The make
function takes an attribute set that supports the options defined in
the request module. Please refer to the module definition for all of the
available options.
The generated shell hook will link the generated configuration file to one of two places:
$PRJ_ROOT
is defined, the file will be linked to $PRJ_ROOT/{output}
where output
is the relative path defined in the call to make
.$PRJ_ROOT
is not defined, the file will be linked to ./{output}
, where
the relative path is determined by where the Nix CLI was invoked.For example, if $PRJ_ROOT
is set to /home/user/code/myprj
and the output is
specified as configs/config.json
then the file will be linked to
/home/user/code/myprj/configs/config.json
.
The shell hook is designed to be integrated into a development shell:
{
# ...
devShells = {
default = pkgs.mkShell {
shellHook = (nixago.lib.make config).shellHook;
};
};
# ...
}
This will ensure the file is generated and linked when you enter the shell. The behavior of the hook can be modified via the available options.
See the documentation for further configuration details.
An additional repository is available that provides extensions for Nixago. These simplify the process of generating configuration files for common development tools.
Tests are run with:
nix flake check
Read this, check out the issues for items needing attention or submit your own, and then: