Merge pull request #76 from jD91mZM2/flake

Flake support
This commit is contained in:
Lassulus 2020-11-09 22:33:33 +01:00 committed by GitHub
commit 450f39121c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 130 additions and 29 deletions

1
.envrc Normal file
View File

@ -0,0 +1 @@
eval "$(lorri direnv)"

View File

@ -1,14 +1,3 @@
{ pkgs ? import <nixpkgs> {} }:
with pkgs;
stdenv.mkDerivation {
name = "nixos-generators";
src = lib.cleanSource ./.;
meta.description = "Collection of image builders";
nativeBuildInputs = [ makeWrapper ];
installFlags = [ "PREFIX=$(out)" ];
postFixup = ''
wrapProgram $out/bin/nixos-generate \
--prefix PATH : ${lib.makeBinPath [ jq coreutils findutils nix ] }
'';
}
(import (builtins.fetchTarball https://github.com/edolstra/flake-compat/archive/master.tar.gz) {
src = ./.;
}).defaultNix.default

25
flake.lock Normal file
View File

@ -0,0 +1,25 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1603153815,
"narHash": "sha256-uCav0CJ0Zm0vbqJiS9NUYD4XZg4Ww9bbsFzcDUzh2+U=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "007126eef72271480cb7670e19e501a1ad2c1ff2",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

49
flake.nix Normal file
View File

@ -0,0 +1,49 @@
{
description = "nixos-generators - one config, multiple formats";
outputs = { self, nixpkgs }: let
forAllSystems = nixpkgs.lib.genAttrs [ "x86_64-linux" "x86_64-darwin" "i686-linux" "aarch64-linux" ];
in {
# Packages
packages = forAllSystems (system: let
pkgs = nixpkgs.legacyPackages."${system}";
in {
nixos-generators = pkgs.stdenv.mkDerivation {
name = "nixos-generators";
src = ./.;
meta.description = "Collection of image builders";
nativeBuildInputs = with pkgs; [ makeWrapper ];
installFlags = [ "PREFIX=$(out)" ];
postFixup = ''
wrapProgram $out/bin/nixos-generate \
--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 ];
});
# Make it runnable with `nix app`
apps = forAllSystems (system: {
nixos-generate = {
type = "app";
program = "${self.packages."${system}".nixos-generators}/bin/nixos-generate";
};
});
defaultApp = forAllSystems (system: self.apps."${system}".nixos-generate);
};
}

View File

@ -1,5 +1,4 @@
#!/usr/bin/env nix-shell
#! nix-shell -i bash -p jq
#!/usr/bin/env bash
set -euo pipefail
## Configuration
@ -8,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=
@ -29,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
@ -61,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
@ -123,10 +136,18 @@ if [[ ! -f $format_path ]]; then
abort "format file not found: $format_path"
fi
nix_args+=(
-I "nixos-config=$configuration"
-I "format-config=$format_path"
)
nix_args+=(--argstr formatConfig "$(realpath "$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
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
import "${toString nixpkgs}/nixos/lib/eval-config.nix" {
inherit system;
modules = [
module
format-config
configuration
];
}
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
formatConfig
configuration
];
}

3
shell.nix Normal file
View File

@ -0,0 +1,3 @@
(import (builtins.fetchTarball https://github.com/edolstra/flake-compat/archive/master.tar.gz) {
src = ./.;
}).shellNix.default