Skip to content

Commit

Permalink
add a simple warning system
Browse files Browse the repository at this point in the history
  • Loading branch information
tek committed Aug 4, 2024
1 parent 3da6a79 commit c9e273c
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-basic-1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
with:
name: tek
signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
- run: rm -rf /opt/hostedtoolcache
- run: git config --global user.name hix
- run: git config --global user.email [email protected]
- run: nix run .#test-basic-1
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test-basic-2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
with:
name: tek
signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
- run: rm -rf /opt/hostedtoolcache
- run: git config --global user.name hix
- run: git config --global user.email [email protected]
- run: nix run .#test-basic-2
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test-basic-3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
with:
name: tek
signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
- run: rm -rf /opt/hostedtoolcache
- run: git config --global user.name hix
- run: git config --global user.email [email protected]
- run: nix run .#test-basic-3
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test-managed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
with:
name: tek
signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
- run: rm -rf /opt/hostedtoolcache
- run: git config --global user.name hix
- run: git config --global user.email [email protected]
- run: nix run .#test-managed
4 changes: 4 additions & 0 deletions lib/doc/chapters.nix
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@

mod-managed = options.moduleWithout [] "managed" { inherit config lib util; };

mod-ui = options.module "ui" { inherit config lib util; inherit (util) outputs; };

generalModules = [
(options.importMod "systems" { inherit config lib; })
(options.importMod "system" { inherit config lib; })
Expand Down Expand Up @@ -113,6 +115,8 @@
(opt "package" "Package" mod-package)
(opt "general" "General" mod-general)
(text prose.ifd)
(text prose.ui)
(opt "ui" "UI" mod-ui)
];
}
{
Expand Down
6 changes: 6 additions & 0 deletions lib/doc/prose.nix
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,12 @@ in {
exposed.
'';

ui = ''
## User Interface {#ui}
Options for UI behavior:
'';

environments = ''
## Environments {#envs}
Expand Down
51 changes: 51 additions & 0 deletions lib/internal/warn.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{util}: let

inherit (util) config lib;
inherit (util.console) chevrons bold;
inherit (util.console.s) colors;

inherit (config.ui.warnings) all keys;

enabled = key: keys.${key} or all;

warnWith = handler: key: message:
handler (enabled key) ''
${message}
Disable this warning by setting ${bold (colors.yellow ''ui.warnings.keys."${key}" = false;'')}
'';

deprecatedWith = handler: key: desc: replacement: let
suggestion = if replacement == null then "" else " in favor of ${colors.blue replacement}";
message = "${desc} is deprecated${suggestion}.";
in warnWith handler "deprecated.${key}" message;

appHandler = exe: isEnabled: message: let
warning = config.pkgs.writeText "hix-warning" ''
${chevrons} Warning: ${message}
'';
wrapped =
if isEnabled
then util.script "hix-warn-wrapper" ''
echo -e "$(cat ${warning})"
exec ${exe}
''
else exe
;
in util.app wrapped;

warnEval = warnWith lib.warnIf;

deprecatedOutput = category: name:
warnEval "${category}.${name}" "The ${category} .#${name}";

deprecatedApp = name: replacement: exe:
deprecatedWith (appHandler exe) "app.${name}" "The app ${colors.blue ".#${name}"}" replacement;

in {
inherit
warnWith
deprecatedWith
deprecatedOutput
deprecatedApp
;
}
6 changes: 3 additions & 3 deletions lib/outputs/hpack.nix
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{util}: let

inherit (util) config;
inherit (util) config internal;

in {

legacyApps = {
hpack = config.hpack.script;
hpack-quiet = config.hpack.scriptQuiet;
hpack = internal.warn.deprecatedApp "hpack" ".#gen-cabal" config.hpack.script;
hpack-quiet = internal.warn.deprecatedApp "hpack-quiet" ".#gen-cabal-quiet" config.hpack.scriptQuiet;
};

}
1 change: 1 addition & 0 deletions modules/all-modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let
./hackage.nix
./cli.nix
./managed.nix
./ui.nix
inputsConfig
];

Expand Down
34 changes: 34 additions & 0 deletions modules/ui.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{ lib, ... }: let

inherit (lib) types;

in {

options = {

ui.warnings = {

all = lib.mkOption {
description = ''
Whether to enable all warnings.
Set this to `false` to suppress all warnings and use [](#opt-ui-ui.warnings.keys) to enable them
selectively.
'';
type = types.bool;
default = true;
};

keys = lib.mkOption {
description = ''
The set of warnings that should be enabled or disabled individually.
Keys are warning IDs and values are booleans, where `true` enables a warning and `false` disables it.
If a key is not present in this set, the warning is printed if [](#opt-ui-ui.warnings.all) is `true`.
'';
type = types.attrsOf types.bool;
default = {};
};
};

};

}

0 comments on commit c9e273c

Please sign in to comment.