Merge pull request #366 from nix-community/refactor/flake-parts

refactor(flake-parts): use perSystem for defining inputs, drop makeFl…
This commit is contained in:
DavHau 2022-11-12 00:07:24 +01:00 committed by GitHub
commit 7f1eb4e4e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 56 deletions

View File

@ -17,10 +17,10 @@
flake-parts.lib.mkFlake {inherit self;} {
systems = ["x86_64-linux"];
imports = [dream2nix.flakeModuleBeta];
dream2nix = {
config.projectRoot = ./.;
perSystem = {config, ...}: {
# define an input for dream2nix to generate outputs for
inputs."ripgrep" = {
dream2nix.inputs."ripgrep" = {
source = src;
settings = [{builder = "crane";}];
};

View File

@ -5,44 +5,44 @@
}: let
l = lib // builtins;
d2n = config.dream2nix;
makeArgs = p:
{
inherit (config) systems;
inherit (d2n) config;
}
// p;
outputs = d2n.lib.dlib.mergeFlakes (
l.map
(p: d2n.lib.makeFlakeOutputs (makeArgs p))
(l.attrValues d2n.inputs)
);
# make attrs default, so that users can override them without
# needing to use lib.mkOverride (usually, lib.mkForce)
mkDefaultRecursive = attrs:
l.mapAttrsRecursiveCond
d2n.lib.dlib.isNotDrvAttrs
(_: l.mkDefault)
attrs;
in {
config = {
flake =
# make attrs default, so that users can override them without
# needing to use lib.mkOverride (usually, lib.mkForce)
l.mapAttrsRecursiveCond
d2n.lib.dlib.isNotDrvAttrs
(_: l.mkDefault)
outputs;
dream2nix.outputs = outputs;
perSystem = {
config,
system,
pkgs,
...
}: let
# get output attrs that have systems
systemizedOutputs =
instance = d2n.lib.init {
inherit pkgs;
inherit (d2n) config;
};
outputs =
l.mapAttrs
(_: attrs: attrs.${system})
(
l.filterAttrs
(_: attrs: l.isAttrs attrs && l.hasAttr system attrs)
(_: args: instance.makeOutputs args)
config.dream2nix.inputs;
getAttrFromOutputs = attrName:
l.mkMerge (
l.mapAttrsToList
(_: output: mkDefaultRecursive output.${attrName})
outputs
);
in {
config = {
dream2nix.outputs = systemizedOutputs;
dream2nix = {inherit instance outputs;};
# TODO(yusdacra): we could combine all the resolveImpure here if there are multiple
# TODO(yusdacra): maybe we could rename outputs with the same name to avoid collisions?
packages = getAttrFromOutputs "packages";
devShells = getAttrFromOutputs "devShells";
};
};
};

View File

@ -1,4 +1,5 @@
{
self,
lib,
flake-parts-lib,
...
@ -20,30 +21,11 @@ in {
type = t.submoduleWith {
modules = [../config];
};
default = {};
default = {
projectRoot = self;
};
description = ''
The dream2nix config. This will be applied to all defined `sources`.
You can override this per `source` by specifying `config` for that source:
```nix
sources."name" = {
config.projectSource = ./source;
};
```
'';
};
inputs = l.mkOption {
type = t.attrsOf t.attrs;
default = {};
description = ''
A list of inputs to generate outputs from.
Each one takes the same arguments `makeFlakeOutputs` takes.
'';
};
outputs = l.mkOption {
type = t.attrsOf t.raw;
readOnly = true;
description = ''
The raw outputs that were generated.
The dream2nix config.
'';
};
};
@ -52,11 +34,26 @@ in {
({...}: {
options = {
dream2nix = {
outputs = l.mkOption {
type = t.attrsOf t.raw;
instance = l.mkOption {
type = t.raw;
readOnly = true;
description = ''
The raw outputs that were generated.
The dream2nix instance.
'';
};
inputs = l.mkOption {
type = t.attrsOf t.attrs;
default = {};
description = ''
A list of inputs to generate outputs from.
Each one takes the same arguments `makeOutputs` takes.
'';
};
outputs = l.mkOption {
type = t.lazyAttrsOf (t.lazyAttrsOf t.raw);
readOnly = true;
description = ''
The raw outputs that were generated for each input.
'';
};
};