Skip to content

Commit

Permalink
add packages.*.expose to control the flake outputs each package shoul…
Browse files Browse the repository at this point in the history
…d be included in
  • Loading branch information
tek committed Jun 21, 2024
1 parent d56d218 commit 704946a
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 48 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Add a reusable workflow for managed bounds updates.
* Parse Nix json output messages to diagnose problems; add overrides for installed packages that fail with a bounds
error printed by Cabal.
* Replace `envs.<env>.derivations` by per-output-category flags in `packages.<package>.expose` to control which packages
are included in outputs.

# 0.7.0: Hix Unbound: Majors Apart

Expand Down
13 changes: 11 additions & 2 deletions lib/env.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{util}: let

inherit (util) config;
inherit (util) config lib;

targets = env: if env.packages == null then config.internal.packageNames else env.packages;

Expand Down Expand Up @@ -29,6 +29,15 @@
fi
'';

exposed = purpose: env: let
allow = name: config.packages.${name}.expose.${purpose};
in lib.filter allow (targets env);

derivations = purpose: envName: let
env = config.envs.${envName};
ghc = env.ghc.ghc;
in lib.genAttrs (exposed purpose env ++ config.output.extraPackages) (n: ghc.${n} // { inherit ghc; });

in {
inherit targets waitScript;
inherit targets waitScript derivations;
}
42 changes: 24 additions & 18 deletions lib/output.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ lib, config, util, ... }:
{util}:
let
inherit (util) app;
inherit (util) app lib config;

envCommand = import ./command.nix { inherit config util; };

Expand Down Expand Up @@ -68,21 +68,28 @@ let
ghc = env.ghc;
in withStatic ghc.ghc.${name} // { cross = cross ghc name; musl = nativeMusl ghc name; };

envOutputs = v: let
env = config.envs.${v};
in lib.mapAttrs (n: d: (withCross env n)) env.derivations;
envOutputs = purpose: envName: let
env = config.envs.${envName};
in lib.mapAttrs (n: d: if purpose == "packages" then (withCross env n) else d) (util.env.derivations purpose envName);

fullEnvOutputs = v: let
env = config.envs.${v};
in envOutputs v // util.attrsetMain (main: {
static = staticDrv env.derivations.${main};
}) // envExes env;
fullEnvOutputs = purpose: envName: let
env = config.envs.${envName};
drvs = util.env.derivations purpose envName;
in
envOutputs purpose envName //
util.attrsetMain (main: { static = staticDrv drvs.${main}; }) //
envExes env
;

prefixed = purpose: envName:
lib.mapAttrs' (n: d: { name = "${envName}-${n}"; value = d; }) (envOutputs purpose envName);

prefixedInEnv = v: lib.mapAttrs' (n: d: { name = "${v}-${n}"; value = d; }) (envOutputs v);
prefixedInEnvs = purpose: envs: util.mapListCatAttrs (prefixed purpose) envs;

prefixedInEnvs = envs: util.mapListCatAttrs prefixedInEnv envs;
scopedEnvOutputs = envs: lib.genAttrs envs (envOutputs "scoped");

devOutputs = let
# TODO the main package/exe should probably also respect the targets and expose settings
devPackages = let
env = config.envs.dev;
ghc = env.ghc.ghc;
minGhc = config.envs.min.ghc.ghc;
Expand All @@ -92,16 +99,14 @@ let
min = minGhc.${name};
profiled = profiledGhc.${name};
};
local = lib.mapAttrs extra config.envs.dev.derivations;
local = lib.mapAttrs extra (util.env.derivations "packages" "dev");
in local // util.attrsetMain (main: {
default = local.${main};
min = local.${main}.min;
profiled = local.${main}.profiled;
static = staticDrv local.${main};
});

scopedEnvOutputs = envs: lib.genAttrs envs envOutputs;

envsApi = envs: {
env = lib.mapAttrs (n: e: { inherit (e.ghc) pkgs ghc; ghc0 = e.ghc.vanillaGhc; } // fullEnvOutputs n) envs;
};
Expand Down Expand Up @@ -162,9 +167,10 @@ let

in {
inherit
prefixedInEnvs
envOutputs
scopedEnvOutputs
devOutputs
prefixedInEnvs
devPackages
envsApi
app
appimageApp
Expand Down
2 changes: 2 additions & 0 deletions lib/util.nix
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ let

env = import ./env.nix { inherit util; };

output = import ./output.nix { inherit util; };

hpack = import ./hpack/default.nix { inherit util; };
};

Expand Down
16 changes: 0 additions & 16 deletions modules/env.nix
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,6 @@ let
};
};

ghc = config.ghc.ghc;

extraPackages = genAttrs global.output.extraPackages (n: ghc.${n});

localPackages = genAttrs (util.env.targets config) (n: ghc.${n} // { inherit ghc; });

ghcidMod = if util.minGhc "9.4" config then config.ghc.pkgs.haskell.lib.dontCheck else id;

in {
Expand Down Expand Up @@ -378,14 +372,6 @@ in {
default = false;
};

derivations = mkOption {
description = ''
The derivations for the local Cabal packages using this env's GHC, as well as the
[](#opt-general-output.extraPackages).
'';
type = lazyAttrsOf package;
};

localPackage = mkOption {
description = ''
A function that takes override combinators and a derivation and returns a modified version of that derivation.
Expand Down Expand Up @@ -597,8 +583,6 @@ in {
gen-overrides = mkDefault true;
};

derivations = mkDefault (localPackages // extraPackages);

hostPorts = util.mapListCatAttrs (s: mapAttrs (_: effectiveHostPort) s.ports) resolved;

shell = mkDefault (global.pkgs.stdenv.mkDerivation {
Expand Down
5 changes: 3 additions & 2 deletions modules/hpack.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
let
inherit (lib) types mkOption;

libOutput = import ../lib/output.nix { inherit config lib util; };
libOutput = util.output;

maybeDefaultApp = name: a:
if name == config.defaultApp
Expand Down Expand Up @@ -73,7 +73,8 @@ in {
hpack = {

apps = lib.mkDefault (
util.catAttrs (lib.mapAttrsToList (packageApps config.envs.dev.derivations) config.hpack.internal.packages)
let drvs = util.env.derivations "apps" "dev";
in util.catAttrs (lib.mapAttrsToList (packageApps drvs) config.hpack.internal.packages)
);

script = util.hpack.gen { verbose = true; };
Expand Down
18 changes: 8 additions & 10 deletions modules/output.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ let

showConfig = import ../lib/show-config.nix { inherit config lib util; };

libOutput = import ../lib/output.nix { inherit config lib util; };

genOverrides = import ../lib/gen-overrides.nix { inherit config lib util; };

showOverrides = import ../lib/show-overrides.nix { inherit config lib util; };
Expand Down Expand Up @@ -130,12 +128,12 @@ in {
};

basicEnvApps = optionalAttrs config.output.envApps {
env = util.mapListCatAttrs libOutput.envApps (lib.attrValues util.visibleAppEnvs);
env = util.mapListCatAttrs util.output.envApps (lib.attrValues util.visibleAppEnvs);
};

lowPrio = {

legacyPackages = libOutput.scopedEnvOutputs config.ghcVersions // libOutput.envsApi config.envs // {
legacyPackages = util.output.scopedEnvOutputs config.ghcVersions // util.output.envsApi config.envs // {
inherit config;
inherit (config.envs.dev.ghc) pkgs ghc;
ghc0 = config.envs.dev.ghc.vanillaGhc;
Expand All @@ -145,20 +143,20 @@ in {
apps = config.hackage.output.apps //
basicApps //
util.managed.output.apps //
libOutput.mainAppimageApp //
optionalAttrs config.output.commandApps { cmd = libOutput.commandApps config.commands; } //
util.output.mainAppimageApp //
optionalAttrs config.output.commandApps { cmd = util.output.commandApps config.commands; } //
basicEnvApps //
util.managed.output.gen
;

};

highPrio = {
packages = libOutput.devOutputs // libOutput.prefixedInEnvs config.ghcVersions;
packages = util.output.devPackages // util.output.prefixedInEnvs "packages" config.ghcVersions;

checks =
config.envs.dev.derivations //
optionalAttrs config.compat.enable (libOutput.prefixedInEnvs config.compat.versions) //
util.output.envOutputs "checks" "dev" //
optionalAttrs config.compat.enable (util.output.prefixedInEnvs "compat" config.compat.versions) //
util.managed.output.checks
;

Expand All @@ -174,7 +172,7 @@ in {
apps = let
exposedCommands = lib.filterAttrs (_: c: c.expose) config.commands;
in
libOutput.commandApps exposedCommands //
util.output.commandApps exposedCommands //
{
gen-cabal = app "${config.hpack.script}";
gen-cabal-quiet = app "${config.hpack.scriptQuiet}";
Expand Down
43 changes: 43 additions & 0 deletions modules/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,49 @@ in {

};

expose = {

packages = mkOption {
description = "Whether to expose this package in [](#opt-output-outputs.packages).";
type = bool;
default = true;
};

apps = mkOption {
description = "Whether to expose this package in [](#opt-output-outputs.apps) if it has executables.";
type = bool;
default = true;
};

checks = mkOption {
description = "Whether to expose this package in [](#opt-output-outputs.checks).";
type = bool;
default = true;
};

compat = mkOption {
description = ''
Whether to expose this package in [](#opt-output-outputs.checks) for GHC version
[compat checks](#opt-general-compat).
'';
type = bool;
default = true;
};

scoped = mkOption {
description = ''
Whether to expose this package in [](#opt-output-outputs.legacyPackages) in env-keyed subsets like
`ghc98.<pkgname>`.
These are not evaluated by `nix flake check` and only buildable when referring to the whole path, like
`nix build .#ghc98.<pkgname>`, so they can include packages you don't want evaluated or built frequently
(because they pull in expensive dependencies etc).
'';
type = bool;
default = true;
};

};

};

config = {
Expand Down

0 comments on commit 704946a

Please sign in to comment.