Synthetica9 / nix-linter

Linter for the Nix expression language
BSD 3-Clause "New" or "Revised" License
158 stars 16 forks source link

False positive UnnededRec when 'chaining' inherit #59

Open chriswarbo opened 3 years ago

chriswarbo commented 3 years ago

Similar to https://github.com/Synthetica9/nix-linter/issues/43

The following code defines an attrset containing lib (taken from import <nixpkgs> {}) and escapeShellArg (taken from lib):

rec {
  inherit (import <nixpkgs> {}) lib;
  inherit (lib) escapeShellArg;
}
Welcome to Nix version 2.3.11. Type :? for help.

nix-repl> import ./example.nix
{ escapeShellArg = «lambda @ /nix/store/xdk4f76azzwg5qami5w250kh9l79sszh-nixpkgs-src/lib/strings.nix:318:20»; lib = { ... }; }

This needs rec, otherwise we can't inherit from the lib variable.

Welcome to Nix version 2.3.11. Type :? for help.

nix-repl> import ./example.nix
error: undefined variable 'lib' at /Users/chrisw/DeleteMe/example.nix:3:12

However, nix-linter thinks the rec is not needed:

$ nix-linter example.nix 
Unneeded `rec` on set at example.nix:1:1-4:2

For context, I often use this pattern alongside with, e.g. a non-toy example:

with rec {
  inherit (builtins) isList toJSON;
  inherit (nixpkgs)
    bash
    callPackage
    coreutils
    gnugrep
    gnused
    jq
    lib
    runCommand
    writeReferencesToFile;
  inherit (lib) concatMapStringsSep escapeShellArg;
  inherit (helpers.util) script;
};
rec {
  ...
}
expipiplus1 commented 3 years ago

I don't have the bandwidth to get to this soon, but would be happy to merge a PR fixing this.

On Sat, Aug 28, 2021 at 3:36 PM chriswarbo @.***> wrote:

Similar to #43 https://github.com/Synthetica9/nix-linter/issues/43

The following code defines an attrset containing lib (taken from import

{}) and escapeShellArg (taken from lib): rec { inherit (import {}) lib; inherit (lib) escapeShellArg; } Welcome to Nix version 2.3.11. Type :? for help. nix-repl> import ./example.nix { escapeShellArg = «lambda @ /nix/store/xdk4f76azzwg5qami5w250kh9l79sszh-nixpkgs-src/lib/strings.nix:318:20»; lib = { ... }; } This needs rec, otherwise we can't inherit from the lib variable. Welcome to Nix version 2.3.11. Type :? for help. nix-repl> import ./example.nix error: undefined variable 'lib' at /Users/chrisw/DeleteMe/example.nix:3:12 However, nix-linter thinks the rec is not needed: $ nix-linter example.nix Unneeded `rec` on set at example.nix:1:1-4:2 For context, I often use this pattern alongside with, e.g. a non-toy example: with rec { inherit (builtins) isList toJSON; inherit (nixpkgs) bash callPackage coreutils gnugrep gnused jq lib runCommand writeReferencesToFile; inherit (lib) concatMapStringsSep escapeShellArg; inherit (helpers.util) script; }; rec { ... } — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub , or unsubscribe . Triage notifications on the go with GitHub Mobile for iOS or Android .
Radvendii commented 2 years ago

@chriswarbo I'm curious; is there a reason you use with rec { ... }; rather than let ...; in?

chriswarbo commented 2 years ago

@Radvendii I wrote up my reasoning at https://github.com/NixOS/nix/issues/1361#issuecomment-390420439 (Warbo is my personal account; chriswarbo is my work account)