Skip to content

Commit

Permalink
Add mergeApps for merging multiple app AttrSets
Browse files Browse the repository at this point in the history
  • Loading branch information
tbidne committed Nov 7, 2024
1 parent 4b8dda0 commit bda7bc3
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,8 @@ jobs:
run: |
cd test
nix run .#lint-yaml
- name: Lint merged
run: |
cd test
nix run .#lint-merged
21 changes: 8 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Nix utility functions for haskell flakes.
}
```

Note that we can also merge multiple apps together, using the `mkDrv` argument (default `true`):
Note that we can also merge multiple apps together, using the `mkDrv` argument (default `true`) and `mergeApps`:

```nix
let
Expand All @@ -135,17 +135,12 @@ let
formatHsNix = nix-hs-utils.format { ... mkDrv = false; ... };
formatYaml = nix-hs-utils.format-yaml { ... mkDrv = false; ... };
# Because of 'mkDrv = false', we need to call mkShellApp ourselves.
format = nix-hs-utils.mkShellApp {
inherit pkgs;
name = "format";
# Now we can combine the command and inputs in a straightforward manner.
text = ''
${formatHsNix.text}
${formatYaml.text}
'';
runtimeInputs = formatHsNix.runtimeInputs ++ formatYaml.runtimeInputs;
# mergeApps takes in a list of app AttrSet and merges them together into a
# single app.
format = nix-hs-utils.mergeApps {
apps = [
(nix-hs-utils.format (compilerPkgs // pkgsMkDrv))
(nix-hs-utils.format-yaml pkgsMkDrv)
];
};
```
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
lint-yaml
mkApp
mkShellApp
mergeApps
;
};
}
84 changes: 81 additions & 3 deletions lib/apps.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,89 @@ let
mkApp (pkgs.writeShellApplication { inherit name text runtimeInputs; });

# Returns either a shell app derivation or the set itself.
drvOrSet = mkDrv: s:
if mkDrv then mkShellApp s else s;
drvOrSet = mkDrv: s: if mkDrv then mkShellApp s else s;

/*
Merges a list of attr sets representing shell apps into a single app.
Fields:
- apps (List AttrSet):
NonEmpty list of AttrSet shell apps. Each app requires the 'text'
and 'runtimeInputs' fields.
- mkDrv (Boolean):
If true (default), returns a derivation i.e. the shell app. Otherwise
returns the merged set.
- name (String):
The name to use for the merged app. If null or unspecified, we take
the name of the __first__ app with a non-null name. At least one name
is required.
- pkgs (AttrSet):
The nixpkgs for creating the shell app. Follows the same semantics
as name.
Example:
a1 = ...
a2 = ...
a3 = ...
app = mergeApps { apps = [a1 a2 a3]; };
Type: mkLibs ::
List AttrSet ->
Boolean ->
Maybe String ->
Maybe AttrSet ->
(AttrSet | Derivation)
*/
mergeApps =
{
apps,
mkDrv ? true,
name ? null,
pkgs ? null,
}:
let
init = {
inherit name pkgs;
text = "";
runtimeInputs = [ ];
};
mergeApp = acc: app: {
# Take the first non-null name we find. Apps are not required to
# have a name, but we need at least one (or top-level) name.
name =
if acc.name != null then
acc.name
else if app ? name then
app.name
else
acc.name;

# Same semantics as name: Take the first non-null or top-level.
pkgs =
if acc.pkgs != null then
acc.pkgs
else if app ? pkgs then
app.pkgs
else
acc.pkgs;

text = ''
${acc.text}
${app.text}
'';
runtimeInputs = acc.runtimeInputs ++ app.runtimeInputs;
};
in
drvOrSet mkDrv (builtins.foldl' mergeApp init apps);
in
{
inherit mkApp mkShellApp;
inherit mkApp mkShellApp mergeApps;

# ShellApp that formats cabal, nix, and haskell via ormolu (default) or
# fourmolu.
Expand Down
11 changes: 11 additions & 0 deletions test/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
compilerPkgs = {
inherit compiler pkgs;
};
pkgsMkDrv = {
inherit pkgs;
mkDrv = false;
};
in
{
packages."${system}".default = mkShell false;
Expand Down Expand Up @@ -87,6 +91,13 @@
};

lint-yaml = nix-hs-utils.lint-yaml { inherit pkgs; };

lint-merged = nix-hs-utils.mergeApps {
apps = [
(nix-hs-utils.lint (compilerPkgs // pkgsMkDrv))
(nix-hs-utils.lint-yaml pkgsMkDrv)
];
};
};
};
}

0 comments on commit bda7bc3

Please sign in to comment.