Merge pull request #339 from nix-community/feat/flake-parts

feat: add a flake-parts module
This commit is contained in:
DavHau 2022-10-31 22:22:15 +01:00 committed by GitHub
commit 4b3a139c50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 163 additions and 5 deletions

View File

@ -0,0 +1,29 @@
{
inputs = {
dream2nix.url = "github:nix-community/dream2nix";
nixpkgs.follows = "dream2nix/nixpkgs";
flake-parts.url = "github:hercules-ci/flake-parts";
src.url = "github:BurntSushi/ripgrep/13.0.0";
src.flake = false;
};
outputs = {
self,
dream2nix,
flake-parts,
src,
...
}:
flake-parts.lib.mkFlake {inherit self;} {
systems = ["x86_64-linux"];
imports = [dream2nix.flakeModuleBeta];
dream2nix = {
config.projectRoot = ./.;
# define an input for dream2nix to generate outputs for
inputs."ripgrep" = {
source = src;
settings = [{builder = "crane";}];
};
};
};
}

View File

@ -194,6 +194,11 @@
# kept for compat
lib2 = d2n-lib;
flakeModuleBeta = {
imports = [./src/modules/flake-parts];
dream2nix.lib = d2n-lib;
};
# all apps including cli, install, etc.
apps = forAllSystems (
system: pkgs:

View File

@ -30,6 +30,7 @@
traceJ
warnIfIfd
parseSpdxId
isNotDrvAttrs
;
inherit

View File

@ -0,0 +1,6 @@
{
imports = [
./interface.nix
./implementation.nix
];
}

View File

@ -0,0 +1,49 @@
{
config,
lib,
...
}: 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)
);
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,
...
}: let
# get output attrs that have systems
systemizedOutputs =
l.mapAttrs
(_: attrs: attrs.${system})
(
l.filterAttrs
(_: attrs: l.isAttrs attrs && l.hasAttr system attrs)
outputs
);
in {
config = {
dream2nix.outputs = systemizedOutputs;
};
};
};
}

View File

@ -0,0 +1,66 @@
{
lib,
flake-parts-lib,
...
}: let
l = lib // builtins;
t = l.types;
in {
options = {
dream2nix = {
lib = l.mkOption {
type = t.raw;
readOnly = true;
description = ''
The system-less dream2nix library.
This should be the the `lib` attribute of the dream2nix flake.
'';
};
config = l.mkOption {
type = t.submoduleWith {
modules = [../config];
};
default = {};
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.
'';
};
};
perSystem =
flake-parts-lib.mkPerSystemOption
({...}: {
options = {
dream2nix = {
outputs = l.mkOption {
type = t.attrsOf t.raw;
readOnly = true;
description = ''
The raw outputs that were generated.
'';
};
};
};
});
};
}

View File

@ -15,17 +15,17 @@
mergeShellConfig = base: other: let
c = base.config;
oc = other.config;
i = base.imports;
oi = other.imports;
i = base.imports or [];
oi = other.imports or [];
hasCConfig = config: (config ? language) && (config.language ? c);
in {
config =
c
// oc
// {
packages = l.unique (c.packages ++ oc.packages);
commands = l.unique (c.commands ++ oc.commands);
env = l.unique (c.env ++ oc.env);
packages = l.unique ((c.packages or []) ++ (oc.packages or []));
commands = l.unique ((c.commands or []) ++ (oc.commands or []));
env = l.unique ((c.env or []) ++ (oc.env or []));
devshell =
(c.devshell or {})
// (oc.devshell or {})
@ -56,6 +56,8 @@
passthru.config = config;
combineWith = other:
mkShell (mergeShellConfig config other.passthru.config);
addConfig = otherConfig:
mkShell (mergeShellConfig config {config = otherConfig;});
};
# convenience utils for converting a nix attribute to a shell env
# TODO: we could probably replace this with something from nixpkgs?