Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative approach #1

Open
svenssonaxel opened this issue Oct 24, 2024 · 0 comments
Open

Alternative approach #1

svenssonaxel opened this issue Oct 24, 2024 · 0 comments

Comments

@svenssonaxel
Copy link

svenssonaxel commented Oct 24, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant