Implement support for flakes

This commit is contained in:
jD91mZM2 2020-10-23 17:54:01 +02:00
parent f8b23a0dd6
commit 9a8e56a767
No known key found for this signature in database
GPG Key ID: E471B167937421AB
3 changed files with 62 additions and 18 deletions

View File

@ -16,25 +16,34 @@
installFlags = [ "PREFIX=$(out)" ];
postFixup = ''
wrapProgram $out/bin/nixos-generate \
--prefix PATH : ${pkgs.lib.makeBinPath (with pkgs; [ jq coreutils findutils nix ])}
--prefix PATH : ${pkgs.lib.makeBinPath (with pkgs; [ jq coreutils findutils ])}
'';
};
# Currently, you need to mark your configurations with makeOverridable in
# order to use nixos-generate on them.
nixosConfigurations.example = nixpkgs.lib.makeOverridable nixpkgs.lib.nixosSystem {
inherit system;
modules = [
./configuration.nix
];
};
});
defaultPackage = forAllSystems (system: self.packages."${system}".nixos-generators);
devShell = forAllSystems (system: let
pkgs = nixpkgs.legacyPackages."${system}";
in pkgs.mkShell {
buildInputs = with pkgs; [ jq coreutils findutils nix ];
buildInputs = with pkgs; [ jq coreutils findutils ];
});
# Make it runnable with `nix app`
apps = forAllSystems (system: {
nixos-generators = {
nixos-generate = {
type = "app";
program = "${self.packages."${system}".nixos-generators}/bin/nixos-generators";
program = "${self.packages."${system}".nixos-generators}/bin/nixos-generate";
};
});
defaultApp = forAllSystems (system: self.apps."${system}".nixos-generators);
defaultApp = forAllSystems (system: self.apps."${system}".nixos-generate);
};
}

View File

@ -7,6 +7,8 @@ readonly libexec_dir="${0%/*}"
readonly format_dir=$libexec_dir/formats
configuration=${NIXOS_CONFIG:-$libexec_dir/configuration.nix}
flake_uri=
flake_attr=
format_path=
target_system=
cores=
@ -28,6 +30,8 @@ Options:
* --help: shows this help
* -c, --configuration PATH:
select the nixos configuration to build. Default: $configuration
* --flake URI:
selects the nixos configuration to build, using flake uri like "~/dotfiles#my-config"
* -f, --format NAME: select one of the pre-determined formats
* --format-path PATH: pass a custom format
* --list: list the available built-in formats
@ -60,6 +64,16 @@ while [[ $# -gt 0 ]]; do
configuration=$2
shift
;;
--flake)
# Note: The reason I'm using awk over cut is because cutting with an
# out-of-bounds field will return the last in-bound field instead of empty
# string.
flake="$(echo "$2" | awk -F'#' '{ print $1; }')"
flake_uri="$(nix flake info --json -- "$flake" | jq -r .url)"
flake_attr="$(echo "$2" | awk -F'#' '{ print $2; }')"
shift
;;
--cores)
cores=$2
shift
@ -122,10 +136,18 @@ if [[ ! -f $format_path ]]; then
abort "format file not found: $format_path"
fi
nix_args+=(--argstr formatConfig "$(realpath "$format_path")")
if [[ -z "$flake_uri" ]]; then
nix_args+=(
-I "nixos-config=$configuration"
-I "format-config=$format_path"
)
else
nix_args+=(
--argstr flakeUri "$flake_uri"
--argstr flakeAttr "${flake_attr:-"$(hostname)"}"
)
fi
if [[ -n $target_system ]]; then
nix_args+=(--argstr system "$target_system")

View File

@ -1,7 +1,11 @@
{ nixpkgs ? <nixpkgs>
, configuration ? <nixos-config>
, format-config ? <format-config>
, system ? builtins.currentSystem
, formatConfig
, flakeUri ? null
, flakeAttr ? null
}:
let
module = { lib, ... }: {
@ -17,12 +21,21 @@ let
};
};
};
# Will only get evaluated when used, so no worries
flake = builtins.getFlake flakeUri;
flakeSystem = flake.outputs.packages."${system}".nixosConfigurations."${flakeAttr}" or flake.outputs.nixosConfigurations."${flakeAttr}";
in
if flakeUri != null then
flakeSystem.override (attrs: {
modules = attrs.modules ++ [ module formatConfig ];
})
else
import "${toString nixpkgs}/nixos/lib/eval-config.nix" {
inherit system;
modules = [
module
format-config
formatConfig
configuration
];
}