2020-10-23 17:48:17 +03:00
|
|
|
{
|
|
|
|
description = "nixos-generators - one config, multiple formats";
|
|
|
|
|
2021-06-17 02:00:15 +03:00
|
|
|
# Lib dependency
|
2021-06-23 20:16:33 +03:00
|
|
|
inputs.nixlib.url = "github:nix-community/nixpkgs.lib";
|
2021-06-17 02:00:15 +03:00
|
|
|
|
|
|
|
# Bin dependency
|
2021-02-10 11:00:13 +03:00
|
|
|
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
|
|
|
|
|
2023-05-29 10:35:13 +03:00
|
|
|
outputs = {
|
|
|
|
self,
|
|
|
|
nixpkgs,
|
|
|
|
nixlib,
|
2023-07-03 13:52:57 +03:00
|
|
|
} @ inputs: let
|
2023-06-25 20:48:53 +03:00
|
|
|
lib = nixpkgs.lib;
|
|
|
|
|
2023-07-03 13:52:57 +03:00
|
|
|
callFlake = flake: let
|
|
|
|
args =
|
|
|
|
inputs
|
|
|
|
// {
|
|
|
|
nixos-generators = self;
|
|
|
|
self = subFlake;
|
|
|
|
};
|
|
|
|
subFlake = (import flake).outputs args;
|
|
|
|
in
|
|
|
|
subFlake;
|
|
|
|
|
2023-06-25 20:48:53 +03:00
|
|
|
# Ensures a derivation's name can be accessed without evaluating it deeply.
|
|
|
|
# Prevents `nix flake show` from being very slow.
|
|
|
|
makeLazyDrv = name: drv: {
|
|
|
|
inherit name;
|
|
|
|
inherit
|
|
|
|
(drv)
|
|
|
|
drvPath
|
|
|
|
outPath
|
|
|
|
outputName
|
2023-09-30 09:56:30 +03:00
|
|
|
system
|
2023-06-25 20:48:53 +03:00
|
|
|
;
|
|
|
|
type = "derivation";
|
|
|
|
};
|
|
|
|
in
|
|
|
|
# Library modules (depend on nixlib)
|
2023-05-29 10:35:13 +03:00
|
|
|
{
|
|
|
|
# export all generator formats in ./formats
|
2023-07-03 13:52:57 +03:00
|
|
|
nixosModules =
|
|
|
|
{
|
|
|
|
all-formats = ./all-formats.nix;
|
|
|
|
}
|
|
|
|
// (nixlib.lib.mapAttrs' (file: _: {
|
|
|
|
name = nixlib.lib.removeSuffix ".nix" file;
|
|
|
|
# The exported module should include the internal format* options
|
|
|
|
value.imports = [(./formats + "/${file}") ./format-module.nix];
|
|
|
|
}) (builtins.readDir ./formats));
|
2023-05-29 10:35:13 +03:00
|
|
|
|
|
|
|
# example usage in flakes:
|
|
|
|
# outputs = { self, nixpkgs, nixos-generators, ...}: {
|
|
|
|
# vmware = nixos-generators.nixosGenerate {
|
|
|
|
# system = "x86_64-linux";
|
|
|
|
# modules = [./configuration.nix];
|
|
|
|
# format = "vmware";
|
|
|
|
# };
|
|
|
|
# }
|
|
|
|
|
|
|
|
nixosGenerate = {
|
|
|
|
pkgs ? null,
|
|
|
|
lib ? nixpkgs.lib,
|
2023-12-04 12:51:22 +03:00
|
|
|
nixosSystem ? nixpkgs.lib.nixosSystem,
|
2023-05-29 10:35:13 +03:00
|
|
|
format,
|
|
|
|
system ? null,
|
|
|
|
specialArgs ? {},
|
|
|
|
modules ? [],
|
|
|
|
customFormats ? {},
|
|
|
|
}: let
|
|
|
|
extraFormats =
|
|
|
|
lib.mapAttrs' (
|
|
|
|
name: value:
|
|
|
|
lib.nameValuePair
|
|
|
|
name
|
|
|
|
(value
|
|
|
|
// {
|
2023-05-29 11:20:56 +03:00
|
|
|
imports = value.imports or [] ++ [./format-module.nix];
|
2023-05-29 10:35:13 +03:00
|
|
|
})
|
|
|
|
)
|
|
|
|
customFormats;
|
|
|
|
formatModule = builtins.getAttr format (self.nixosModules // extraFormats);
|
2023-12-04 12:51:22 +03:00
|
|
|
image = nixosSystem {
|
2023-05-29 10:35:13 +03:00
|
|
|
inherit pkgs specialArgs;
|
|
|
|
system =
|
|
|
|
if system != null
|
|
|
|
then system
|
|
|
|
else pkgs.system;
|
|
|
|
lib =
|
|
|
|
if lib != null
|
|
|
|
then lib
|
|
|
|
else pkgs.lib;
|
|
|
|
modules =
|
|
|
|
[
|
|
|
|
formatModule
|
|
|
|
]
|
|
|
|
++ modules;
|
2021-06-17 02:00:15 +03:00
|
|
|
};
|
2023-05-29 10:35:13 +03:00
|
|
|
in
|
|
|
|
image.config.system.build.${image.config.formatAttr};
|
2021-06-17 02:00:15 +03:00
|
|
|
}
|
2023-05-29 10:35:13 +03:00
|
|
|
//
|
|
|
|
# Binary and Devshell outputs (depend on nixpkgs)
|
|
|
|
(
|
|
|
|
let
|
|
|
|
forAllSystems = nixpkgs.lib.genAttrs ["x86_64-linux" "x86_64-darwin" "i686-linux" "aarch64-linux" "aarch64-darwin"];
|
|
|
|
in {
|
|
|
|
formatter = forAllSystems (system: nixpkgs.legacyPackages.${system}.alejandra);
|
|
|
|
|
|
|
|
packages = forAllSystems (system: let
|
|
|
|
pkgs = nixpkgs.legacyPackages."${system}";
|
|
|
|
in rec {
|
2023-05-29 10:45:01 +03:00
|
|
|
default = nixos-generate;
|
2023-05-29 10:35:13 +03:00
|
|
|
nixos-generate = pkgs.stdenv.mkDerivation {
|
|
|
|
name = "nixos-generate";
|
|
|
|
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])}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2023-05-29 12:01:19 +03:00
|
|
|
checks =
|
2023-07-03 13:52:57 +03:00
|
|
|
lib.recursiveUpdate
|
|
|
|
(callFlake ./checks/test-all-formats-flake/flake.nix).checks
|
2023-05-29 12:01:19 +03:00
|
|
|
(
|
2024-01-16 13:00:01 +03:00
|
|
|
lib.genAttrs [
|
|
|
|
"x86_64-linux"
|
|
|
|
# We currently don't have kvm support on our builder
|
|
|
|
#"aarch64-linux"
|
|
|
|
]
|
2023-07-03 13:52:57 +03:00
|
|
|
(
|
|
|
|
system: let
|
|
|
|
allFormats = import ./checks/test-all-formats.nix {
|
|
|
|
inherit nixpkgs system;
|
|
|
|
};
|
|
|
|
test-customize-format = import ./checks/test-customize-format.nix {
|
|
|
|
inherit nixpkgs system;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
lib.mapAttrs makeLazyDrv (
|
|
|
|
{
|
|
|
|
inherit
|
|
|
|
(self.packages.${system})
|
|
|
|
nixos-generate
|
|
|
|
;
|
|
|
|
|
|
|
|
inherit test-customize-format;
|
|
|
|
|
|
|
|
is-formatted = import ./checks/is-formatted.nix {
|
|
|
|
pkgs = nixpkgs.legacyPackages.${system};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// allFormats
|
|
|
|
)
|
|
|
|
)
|
2023-05-29 12:01:19 +03:00
|
|
|
);
|
2023-05-29 10:35:13 +03:00
|
|
|
|
2023-05-29 10:50:23 +03:00
|
|
|
devShells = forAllSystems (system: let
|
2023-05-29 10:35:13 +03:00
|
|
|
pkgs = nixpkgs.legacyPackages."${system}";
|
2023-05-29 10:50:23 +03:00
|
|
|
in {
|
|
|
|
default = pkgs.mkShell {
|
2023-05-29 10:35:13 +03:00
|
|
|
buildInputs = with pkgs; [jq coreutils findutils];
|
2023-05-29 10:50:23 +03:00
|
|
|
};
|
|
|
|
});
|
2023-05-29 10:35:13 +03:00
|
|
|
|
|
|
|
# Make it runnable with `nix run`
|
|
|
|
apps = forAllSystems (system: let
|
|
|
|
nixos-generate = {
|
|
|
|
type = "app";
|
|
|
|
program = "${self.packages."${system}".nixos-generate}/bin/nixos-generate";
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
inherit nixos-generate;
|
|
|
|
|
|
|
|
# Nix >= 2.7 flake output schema uses `apps.<system>.default` instead
|
|
|
|
# of `defaultApp.<system>` to signify the default app (the thing that
|
|
|
|
# gets run with `nix run . -- <args>`)
|
|
|
|
default = nixos-generate;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2020-10-23 17:48:17 +03:00
|
|
|
}
|