nix-linter icon indicating copy to clipboard operation
nix-linter copied to clipboard

False positive UnnededRec when 'chaining' inherit

Open chriswarbo opened this issue 4 years ago • 3 comments

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 {
  ...
}

chriswarbo avatar Aug 28 '21 14:08 chriswarbo

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 https://github.com/Synthetica9/nix-linter/issues/59, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGRJXGTK5NNELK2KUHVRKLT7DX4HANCNFSM5C7FZ3TA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

expipiplus1 avatar Aug 31 '21 18:08 expipiplus1

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

Radvendii avatar Nov 23 '21 19:11 Radvendii

@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)

chriswarbo avatar Nov 23 '21 20:11 chriswarbo