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)" ]; installFlags = [ "PREFIX=$(out)" ];
postFixup = '' postFixup = ''
wrapProgram $out/bin/nixos-generate \ 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); defaultPackage = forAllSystems (system: self.packages."${system}".nixos-generators);
devShell = forAllSystems (system: let devShell = forAllSystems (system: let
pkgs = nixpkgs.legacyPackages."${system}"; pkgs = nixpkgs.legacyPackages."${system}";
in pkgs.mkShell { in pkgs.mkShell {
buildInputs = with pkgs; [ jq coreutils findutils nix ]; buildInputs = with pkgs; [ jq coreutils findutils ];
}); });
# Make it runnable with `nix app` # Make it runnable with `nix app`
apps = forAllSystems (system: { apps = forAllSystems (system: {
nixos-generators = { nixos-generate = {
type = "app"; 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 readonly format_dir=$libexec_dir/formats
configuration=${NIXOS_CONFIG:-$libexec_dir/configuration.nix} configuration=${NIXOS_CONFIG:-$libexec_dir/configuration.nix}
flake_uri=
flake_attr=
format_path= format_path=
target_system= target_system=
cores= cores=
@ -28,6 +30,8 @@ Options:
* --help: shows this help * --help: shows this help
* -c, --configuration PATH: * -c, --configuration PATH:
select the nixos configuration to build. Default: $configuration 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 * -f, --format NAME: select one of the pre-determined formats
* --format-path PATH: pass a custom format * --format-path PATH: pass a custom format
* --list: list the available built-in formats * --list: list the available built-in formats
@ -60,6 +64,16 @@ while [[ $# -gt 0 ]]; do
configuration=$2 configuration=$2
shift 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)
cores=$2 cores=$2
shift shift
@ -122,10 +136,18 @@ if [[ ! -f $format_path ]]; then
abort "format file not found: $format_path" abort "format file not found: $format_path"
fi fi
nix_args+=( nix_args+=(--argstr formatConfig "$(realpath "$format_path")")
-I "nixos-config=$configuration"
-I "format-config=$format_path" if [[ -z "$flake_uri" ]]; then
) nix_args+=(
-I "nixos-config=$configuration"
)
else
nix_args+=(
--argstr flakeUri "$flake_uri"
--argstr flakeAttr "${flake_attr:-"$(hostname)"}"
)
fi
if [[ -n $target_system ]]; then if [[ -n $target_system ]]; then
nix_args+=(--argstr system "$target_system") nix_args+=(--argstr system "$target_system")

View File

@ -1,7 +1,11 @@
{ nixpkgs ? <nixpkgs> { nixpkgs ? <nixpkgs>
, configuration ? <nixos-config> , configuration ? <nixos-config>
, format-config ? <format-config>
, system ? builtins.currentSystem , system ? builtins.currentSystem
, formatConfig
, flakeUri ? null
, flakeAttr ? null
}: }:
let let
module = { lib, ... }: { 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 in
import "${toString nixpkgs}/nixos/lib/eval-config.nix" { if flakeUri != null then
inherit system; flakeSystem.override (attrs: {
modules = [ modules = attrs.modules ++ [ module formatConfig ];
module })
format-config else
configuration import "${toString nixpkgs}/nixos/lib/eval-config.nix" {
]; inherit system;
} modules = [
module
formatConfig
configuration
];
}