Skip to content

Commit

Permalink
add dep-versions app
Browse files Browse the repository at this point in the history
  • Loading branch information
tek committed Sep 2, 2023
1 parent 975f83d commit 40e8aae
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Unreleased

* Allow envs to be excluded from being exposed as `devShells` for specific systems.
* Add an app, `dep-versions`, that prints all components' direct dependencies and their actual version in an
environment.

# 0.6.0

Expand Down
3 changes: 3 additions & 0 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ let
asFunction = f:
if isFunction f then f else _: f;

concatMapAttrsToList = f: a: concatLists (mapAttrsToList f a);

unwords = concatStringsSep " ";

unlines = concatStringsSep "\n";
Expand Down Expand Up @@ -126,6 +128,7 @@ in {
normalizeOverrides
overridesFor
asFunction
concatMapAttrsToList
unwords
unlines
unlinesMap
Expand Down
49 changes: 49 additions & 0 deletions lib/dep-versions.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{config, lib, util, env}:
with lib;
let

console = import ./console.nix { inherit lib; };
inherit (console) s indent;
inherit (s) colors;

pkgs = config.pkgs;

ghc = config.envs.${env}.ghc.ghc;

dep' = name: version: let
desc = if version == null then colors.magenta name else "${colors.magenta name} ${colors.cyan version}";
pkg = ghc.${name};
actual =
if pkg == null
then "[boot package or unknown]"
else "${colors.green "->"} ${colors.red pkg.version}";
in ["${desc} ${actual}"];

dep = d:
if isAttrs d
then dep' d.name d.version
else let
result = builtins.split "^([[:graph:]]+) (.*)$" d;
parts = head (drop 1 result);
name = head parts;
version = head (drop 1 parts);
in
if length result == 3
then dep' name version
else dep' d null;

componentDeps = name: conf: let
head = colors.yellow name;
in [head] ++ indent (concatMap dep conf.dependencies);

packageDeps = name: conf: let
head = colors.blue name;
in [head] ++ indent (util.concatMapAttrsToList componentDeps conf.internal.componentsSet);

packagesDeps =
pkgs.writeText "dep-versions" (util.unlines (util.concatMapAttrsToList packageDeps config.packages));

in pkgs.writeScript "print-dep-versions" ''
#!${pkgs.zsh}/bin/zsh
echo -e "$(cat ${packagesDeps})"
''
1 change: 1 addition & 0 deletions lib/doc/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ let
(opt "hackage" "Hackage" mod-hackage)
(text tags)
(text cross)
(text misc)
];
}
];
Expand Down
30 changes: 30 additions & 0 deletions lib/doc/prose.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1030,4 +1030,34 @@ in {
'';

misc = ''
## Miscellaneous tools {#misc}
### Show overrides {#show-overrides}
```
nix run .#show-overrides
```
Prints all environments' overrides.
### Show config {#show-config}
```
nix run .#show-config
```
Prints the project's entire configuration.
### Show dependency versions {#dep-versions}
```
nix run .#dep-versions
nix run .#env.ghc96.dep-versions
```
Prints all components' dependencies and their actual versions in the dev environment, or the named environment in the
second variant.
'';

}
7 changes: 6 additions & 1 deletion modules/output.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ let

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

depVersions = env: import ../lib/dep-versions.nix { inherit config lib util env; };

# TODO use the json method and print in cli
show-config = util.paramApp {
name = "show-config";
Expand All @@ -25,7 +27,9 @@ let
app = program: { type = "app"; program = "${program}"; };

envApps = env: {
${env.name} = mapAttrs (_: command: app "${(envCommand { inherit env command; }).path}") config.commands;
${env.name} = mapAttrs (_: command: app "${(envCommand { inherit env command; }).path}") config.commands // {
dep-versions = app "${depVersions env.name}";
};
};

commandApps = mapAttrs (_: c: app "${c.path}");
Expand Down Expand Up @@ -136,6 +140,7 @@ in {
gen-overrides = app "${genOverrides}";
gen = app "${genAll}";
show-overrides = app "${showOverrides}";
dep-versions = app "${depVersions "dev"}";
};

};
Expand Down
1 change: 1 addition & 0 deletions test/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ let
overrides = test "overrides";
subdir = test "subdir";
local-prelude = test "local-prelude";
dep-versions = test "dep-versions";
};

testA = n: t: "${n} ${t}";
Expand Down
1 change: 1 addition & 0 deletions test/dep-versions/root/dep/lib/Dep.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module Dep where
35 changes: 35 additions & 0 deletions test/dep-versions/root/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
description = "hix test project";

inputs.hix.url = "path:HIX";

outputs = { hix, ... }:
hix.lib.flake {
main = "root";
packages = {

dep = {
src = ./dep;
cabal = {
dependencies = ["aeson >= 2" "extra ^>= 1.7"];
};
library = {
enable = true;
dependencies = ["uuid == 1.3" "vector >= 0.11 && < 0.13"];
};
};

root = {
src = ./.;

library = {
enable = true;
dependencies = [{ name = "aeson"; version = "^>= 2.1"; } "array"];
};

};

};

};
}
1 change: 1 addition & 0 deletions test/dep-versions/root/lib/Root.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module Root where
19 changes: 19 additions & 0 deletions test/dep-versions/test.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{ pkgs }: {
test = builtins.toFile "dep-versions-test" ''
cd ./root
nix flake update
target='dep
library
aeson >= 2 -> 2.1.2.1
extra ^>= 1.7 -> 1.7.14
uuid == 1.3 -> 1.3.15
vector >= 0.11 && < 0.13 -> 0.13.0.0
root
library
aeson ^>= 2.1 -> 2.1.2.1
array [boot package or unknown]'
check 'nix run .#dep-versions' $target 'Output is wrong'
'';
}

0 comments on commit 40e8aae

Please sign in to comment.