Skip to content

Commit

Permalink
extract "exports" & organize code
Browse files Browse the repository at this point in the history
  • Loading branch information
David Arnold committed Jan 11, 2021
1 parent 534f13e commit 5c8627c
Showing 1 changed file with 93 additions and 119 deletions.
212 changes: 93 additions & 119 deletions nixosFlake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# pass an instance of self
self
, # pass an instance of the nixpkgs flake used to determine your nixos version
nixpkgs-os
nixpkgsOS
, # we assume that the name maps to the project name, and also that the
# overlay has an attribute with the `name` prefix that contains all of the
# project's packages.
Expand All @@ -13,7 +13,7 @@
nixpkgsAlt ? null
, # list of strings that represent a package path; pass either a list or a path to one
backportFromAlt ? []
, # nixpkgs config for nixpkgs-os & nixpkgs-unstable
, # nixpkgs config for nixpkgsOS & nixpkgs-unstable
config ? {}
, # pass either an attribute set or a path to one
hosts ? null
Expand All @@ -24,13 +24,13 @@
, # use this to load other flakes modules to supplement your overlays
externOverlays ? []
, # main (or only) local nixos configuration module; pass either a nixos module or a path to one
nixosModules ? null
nixosModule ? null
, # local nixos configuration modules; pass either an attrset of module or a path to one to each leaf attr
nixosModules ? {}
, # use this to load other flakes modules to supplement your configuration
externNixosModules ? []
, # main (or only) local numtide/devshell configuration module; pass either a devshell module or a path to one
devshellModules ? null
devshellModule ? null
, # local numtide/devshell configuration modules; pass either a devshell module or a path to one to each leaf attr
devshellModules ? {}
, # use this to load other flakes modules to supplement your devshells
Expand All @@ -51,78 +51,97 @@ let
obj
;

exports = {
# let others kick-start from your configuration
templates."${name}" = {
path = self;
description = "template based on ${name}'s configuration";
};
defaultTemplate = self.templates.${name};
}
# share your name-spaced (mainly) custom packages as overlay (convention)
// (
if overlay != null then {
overlay = maybeImport overlay;
} else {}
)
# share non-name-spaced custom (mainly non packages) overlays (convention)
// (
if overlays != {} then {
overlays = maybeImportValues (lib.flattenTree (maybeImport overlays));
} else {}
)
# share your primary (or only) nixos configurations as module
// (
if nixosModule != null then {
nixosModule = maybeImport nixosModule;
} else {}
)
# share your nixos configurations as modules
// (
if nixosModules != {} then {
nixosModules = maybeImportValues (lib.flattenTree (maybeImport nixosModules));
} else {}
)
# share your primary (or only) devshell configurations as module
// (
if devshellModule != null then {
devshellModule = maybeImport devshellModule;
} else {}
)
# share your devshell configurations as modules
// (
if devshellModules != null then {
devshellModules = maybeImportValues (lib.flattenTree (maybeImport devshellModules));
} else {}
);

outputs = lib.eachSystem systems (
system:
let
overlays_ =
externOverlays
++ (
if self ? "overlay" then
[ self.overlay ]
else
[]
)
++ (
if self ? "overlays" then
builtins.attrValues self.overlays
else
[]
)
;
overlays' = let
backportFromAlt' = maybeImport backportFromAlt;
pkgsAlt = import nixpkgsAlt {
inherit
config
system
;
};
resolveKey = key:
let
attrs = builtins.filter builtins.isString (builtins.split "\\." key);
op = sum: attr: sum.${attr} or (throw "package \"${key}\" not found");
in
builtins.foldl' op pkgs attrs
;
overlay = pkgKey: final: prev: {
pkgKey = resolveKey pkgKey;
};
in
overlays_ ++ (map overlay backportFromAlt');

nixosModules' =
externNixosModules
++ (
if self ? "nixosModule" then
[ self.nixosModules ]
else
[]
)
++ (
if self ? "nixosModules" then
builtins.attrValues self.nixosModules
else
[]
)
++ (if self ? "nixosModule" then [ self.nixosModule ] else [])
++ (if self ? "nixosModules" then builtins.attrValues self.nixosModules else [])
;

devshellModules' =
externDevshellModules
++ (if self ? "devshellModule" then [ self.devshellModule ] else [])
++ (if self ? "devshellModules" then builtins.attrValues self.devshellModules else [])
;

overlays' =
externOverlays
++ (if self ? "overlay" then [ self.overlay ] else [])
++ (if self ? "overlays" then builtins.attrValues self.overlays else [])
++ (
if self ? "devshellModule" then
[ self.devshellModules ]
else
[]
)
++ (
if self ? "devshellModules" then
builtins.attrValues self.devshellModules
else
[]
let
resolveKey = pkgs: key:
let
attrs = builtins.filter builtins.isString (builtins.split "\\." key);
op = sum: attr: sum.${attr} or (throw "package \"${key}\" not found");
in
builtins.foldl' op pkgs attrs
;
resolveOverlay = pkgs: pkgKey: final: prev: {
pkgKey = resolveKey pkgs pkgKey;
};
in
let
backportFromAlt' = maybeImport backportFromAlt;
pkgsAlt = import nixpkgsAlt {
inherit
config
system
;
};
in
map resolveOverlay pkgsAlt backportFromAlt'
)
;

pkgs = import nixpkgs-os {
pkgs = import nixpkgsOS {
overlays = overlays';
inherit
config
Expand All @@ -134,74 +153,29 @@ let
in
{
nixosConfigurations =
import ./hosts (
recursiveUpdate inputs {
inherit lib pkgs system utils externModules;
}
);
maybeImportValues (lib.flattenTree (maybeImport hosts))
(
recursiveUpdate inputs {
inherit lib pkgs system utils externModules;
}
);

legacyPackages = packages;

packages = lib.filterAttrs
# Filter on broken packages - you dont't want to expose them to the world
(_: drv: drv.meta.broken != true)
lib.flattenTreeSystem system packages;
}
// (
} // (
if packages ? defaultPackage then {
defaultPackage = packages.defaultPackage;
} else {}
)
// (
) // (
if shell != null then {
devShell = maybeImport shell { inherit pkgs; };
} else if packages ? devShell then {
devShell = packages.devShell;
} else {}
)
# share your name-spaced (mainly) custom packages as overlay (convention)
// (
if overlay != null then {
overlay = maybeImport overlay;
} else {}
)
# share non-name-spaced custom (mainly non packages) overlays (convention)
// (
if overlays != {} then {
overlays = maybeImportValues (lib.flattenTreeSystem system (maybeImport overlays));
} else {}
)
# share your primary (or only) nixos configurations as module
// (
if nixosModule != null then {
nixosModule = maybeImport nixosModule;
} else {}
)
# share your nixos configurations as modules
// (
if nixosModules != {} then {
nixosModules = maybeImportValues (lib.flattenTreeSystem system (maybeImport nixosModules));
} else {}
)
# share your primary (or only) devshell configurations as module
// (
if devshellModule != null then {
devshellModule = maybeImport devshellModule;
} else {}
)
# share your devshell configurations as modules
// (
if devshellModules != null then {
devshellModules = maybeImportValues (lib.flattenTreeSystem system (maybeImport devshellModules));
} else {}
)
# let others kick-start from your configuration
// (
templates.${name} = {
path = self;
description = "template based on ${name}'s configuration";
};
defaultTemplate = self.templates.${name};
)
in
outputs
);
in
outputs // exports

0 comments on commit 5c8627c

Please sign in to comment.