Skip to content

Commit

Permalink
Use builtins.traceVerbose for logging (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
srid authored Jan 12, 2024
1 parent c9c28dc commit 8a526aa
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- #210: Add `extraLibraries` to `settings` module.
- #215: Improved debug logging.
- #216: Remove `debug` option (pass `--trace-verbose` to nix instead)

## 0.4.0 (Aug 22, 2023)

Expand Down
29 changes: 15 additions & 14 deletions doc/guide/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,20 @@ slug: /haskell-flake/debugging

# Debugging logs

Enabling the `debug` option causes haskell-flake to print verbose logging of its activity. To enable it:

```nix
haskellProjects.default = {
debug = true; # Turn on verbose logging
projectRoot = ./.;
...
}
```
:::warning
This feature is available only in Nix versions 2.10 or later.
:::

Passing `--trace-verbose` to Nix commands causes haskell-flake to print verbose logging of its activity. To enable it:

>[!tip] Timestamps in logs
> `moreutils` provides the `ts` command that you can pipe your nix command output to in order to get timestamps in the logs.
:::tip[Timestamps in logs]
`moreutils` provides the `ts` command that you can pipe your nix command output to in order to get timestamps in the logs.
:::

With debug option enabled, you can execute your `nix` commands piped through `ts` to get timestamps in the debug logs. The below is a sample output when building [haskell-multi-nix](https://github.com/srid/haskell-multi-nix/tree/debug):
The below is a sample output when building [haskell-multi-nix](https://github.com/srid/haskell-multi-nix/tree/debug) with `--trace-verbose`:

```
$ nix build github:srid/haskell-multi-nix/debug#bar 2>&1 | ts '[%Y-%m-%d %H:%M:%S]'
$ nix build --trace-verbose github:srid/haskell-multi-nix#bar 2>&1 | ts '[%Y-%m-%d %H:%M:%S]'
[2024-01-11 15:33:32] trace: DEBUG[haskell-flake] [k0ad89r6pa70rly68ibff1jkw59bljgh-source#haskellProjects.default]: default.findPackagesInCabalProject = {"bar":"/nix/store/k0ad89r6pa70rly68ibff1jkw59bljgh-source/./bar","foo":"/nix/store/k0ad89r6pa70rly68ibff1jkw59bljgh-source/./foo"}
[2024-01-11 15:33:32] trace: DEBUG[haskell-flake] [k0ad89r6pa70rly68ibff1jkw59bljgh-source#haskellProjects.default]: defaults.packages = {"bar":{"imports":[{"_file":"/nix/store/n2nb5achv6p0bv3nvqw731mfr907d8ny-source/nix/modules/project/defaults.nix, via option perSystem.aarch64-darwin.haskellProjects.default.defaults.packages.bar","imports":[{"source":"/nix/store/k0ad89r6pa70rly68ibff1jkw59bljgh-source/./bar"}]}]},"foo":{"imports":[{"_file":"/nix/store/n2nb5achv6p0bv3nvqw731mfr907d8ny-source/nix/modules/project/defaults.nix, via option perSystem.aarch64-darwin.haskellProjects.default.defaults.packages.foo","imports":[{"source":"/nix/store/k0ad89r6pa70rly68ibff1jkw59bljgh-source/./foo"}]}]}}
[2024-01-11 15:33:32] trace: DEBUG[haskell-flake] [k0ad89r6pa70rly68ibff1jkw59bljgh-source#haskellProjects.default]: bar.getCabalExecutables = bar
Expand All @@ -36,4 +33,8 @@ $ nix build github:srid/haskell-multi-nix/debug#bar 2>&1 | ts '[%Y-%m-%d %H:%M:%
[2024-01-11 15:33:35] trace: DEBUG[haskell-flake] [k0ad89r6pa70rly68ibff1jkw59bljgh-source#haskellProjects.default]: foo.cabal2nixDeriver /nix/store/i36x01zcdpm7c3m3fjjq1qa4slv61jpw-cabal2nix-foo
[2024-01-11 15:33:35] trace: DEBUG[haskell-flake] [k0ad89r6pa70rly68ibff1jkw59bljgh-source#haskellProjects.default]: foo.fromSdist /nix/store/qrsy0bm4khcs1hxy0rhb6m3g2bvi15sm-foo-0.1.0.0
[2024-01-11 15:33:35] trace: DEBUG[haskell-flake] [k0ad89r6pa70rly68ibff1jkw59bljgh-source#haskellProjects.default]: bar.fromSdist /nix/store/anyx51rm5gjdclafcz5is7jpqgfq2i4w-bar-0.1.0.0
```
```

## See also

- Read more about [the `traceVerbose` function](https://nixos.asia/en/traceVerbose) which haskell-flake uses to produce the above logs.
12 changes: 6 additions & 6 deletions nix/logging.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{ name, debug ? false, ... }:
{ name, ... }:

{
traceDebug = msg:
if debug then
builtins.trace ("DEBUG[haskell-flake] [${name}]: " + msg)
else
x: x;
# traceDebug uses traceVerbose; and is no-op on Nix versions below 2.10
traceDebug =
if builtins.compareVersions "2.10" builtins.nixVersion < 0
then msg: builtins.traceVerbose ("DEBUG[haskell-flake] [${name}]: " + msg)
else x: x;

traceWarning = msg:
builtins.trace ("WARNING[haskell-flake] [${name}]: " + msg);
Expand Down
10 changes: 0 additions & 10 deletions nix/modules/project/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,10 @@ in
then builtins.baseNameOf config.projectRoot
else cls;
};
debug = mkOption {
type = types.bool;
default = false;
description = ''
Whether to enable verbose trace output from haskell-flake.
Useful for debugging.
'';
};
log = mkOption {
type = types.attrsOf (types.functionTo types.raw);
default = import ../../logging.nix {
name = config.projectFlakeName + "#haskellProjects." + name;
inherit (config) debug;
};
internal = true;
readOnly = true;
Expand Down
2 changes: 0 additions & 2 deletions test/simple/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
haskellProjects.default = {
# Multiple modules should be merged correctly.
imports = [ self.haskellFlakeProjectModules.default ];
# Debug logging should work.
debug = true;
packages = {
# Because the module being imported above also defines a root for
# the 'foo' package, we must override it here using `lib.mkForce`.
Expand Down

0 comments on commit 8a526aa

Please sign in to comment.