Skip to content

Alternative approach #1

@svenssonaxel

Description

@svenssonaxel

Nice workaround! After playing around with it, I ended up using a different approach.

The main advantage with this one is that you can still use nix build with all its normal arguments. The main hack is that you configure an output by means of configuring an input. Here's an example flake.nix:

{
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/24.05";
  inputs.flake-utils.url = "github:numtide/flake-utils";
  inputs.configFile = { url = "path:./defaults/config.nix"; flake = false; };
  outputs = { self, nixpkgs, flake-utils, configFile }: let
    params = import configFile;
  in flake-utils.lib.eachDefaultSystem (system:
    import ./main.nix { inherit system nixpkgs params });
}

So the file ./defaults/config.nix will be hashed and mentioned in flake.lock. Now, key excerpts from a wrapper build.sh:

show_help() {
cat <<EOF
Usage: $0 [--compiler COMPILER] [ --prod ] \\
         [ --error-insert | --no-error-insert ] [ -- [ BUILD_ARGUMENTS ] ]
       $0 --help

...

Options:
  --compiler COMPILER                  Specify the compiler version to use.
  --prod                               Make a production build. Default is debug
                                       build.
  --error-insert, --no-error-insert    Activate error insert functionality.
                                       Default is on for debug and off for prod
                                       builds.
  -h, --help                           Show this help message and exit.
  BUILD_ARGUMENTS                      Arbitrary arguments to pass along to
                                       'nix build'.
EOF
}

...


tmpConfigfile=$(mktemp)
{
  echo "{"
  echo "  compiler = $COMPILER;"
  echo "  prodBuild = $PROD_BUILD;"
  echo "  errorInsert = $ERROR_INSERT;"
  echo "}"
} > "$tmpConfigfile"
configfile="$(nix store add "$tmpConfigfile" -n config.nix)"
rm "$tmpConfigfile"

nix build $FLAKE --override-input configFile path:$configfile "$@"

So the parameters are passed in by means of an overridden, non-flake input. The flake then passes these on to whatever function takes them, and uses the (already configured) as a normal, "non-configurable" output.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions