Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite Recursion With stdenv functions #414

Closed
nocoolnametom opened this issue Oct 3, 2018 · 7 comments
Closed

Infinite Recursion With stdenv functions #414

nocoolnametom opened this issue Oct 3, 2018 · 7 comments

Comments

@nocoolnametom
Copy link

nocoolnametom commented Oct 3, 2018

I've made use of pkgs.stdenv.isDarwin and pkgs.stdenv.isLinux to produce a configuration that equally applies to my work MacOS machine and my personal NixOS machine. It's nice because changes that are not OS-specific automatically apply to each system. An example of a configuration that is working:

{ pkgs, lib, ... }:
{
  programs.home-manager.enable = true;
  programs.home-manager.path = https://github.com/rycee/home-manager/master.tar.gz;

  programs.emacs.enable = pkgs.stdenv.isLinux;
  # programs.emacs.<everything_else> = <etc>; # and so on...
}

But what I would like to do is the following, which would help me to remove all of the OS-specifics, especially enable parameters, to the same location instead of being spread throughout the configuration:

{ pkgs, lib, ... }:
{
  programs.home-manager.enable = true;
  programs.home-manager.path = https://github.com/rycee/home-manager/master.tar.gz;
  
  # programs.emacs.<everything_else> = <etc>; # and so on...
} 
// (lib.optionalAttrs pkgs.stdenv.isLinux {
  programs.emacs.enable = true;
}) 
// (lib.optionalAttrs pkgs.stdenv.isDarwin {
  programs.emacs.enable = false;
})

This fails, however:

$ home-manager -v build
error: while evaluating the attribute 'text' of the derivation 'news-info.sh' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:148:11:
while evaluating the attribute 'newsEntries' at /nix/store/bjm2jgn200z2w6619f5rchv8r2jiwc8v-master.tar.gz/modules/default.nix:49:3:
while evaluating the attribute 'config.news.entries' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:87:25:
while evaluating 'yieldConfig' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:74:29, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:73:16:
while evaluating 'mergeModules' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:190:26, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:64:17:
while evaluating 'mergeModules'' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:194:36, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:191:5:
while evaluating 'concatMap' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/lists.nix:104:18, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:230:9:
while evaluating 'reverseList' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/lists.nix:281:17, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:64:38:
while evaluating 'filterModules' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:93:31, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:64:51:
while evaluating 'closeModules' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:101:27, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:62:16:
while evaluating anonymous function at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/lists.nix:96:29, called from undefined position:
while evaluating anonymous function at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:103:50, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/lists.nix:96:32:
while evaluating 'unifyModuleSyntax' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:118:34, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:109:11:
while evaluating 'applyIfFunction' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:144:29, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:109:39:
while evaluating anonymous function at /Users/tdoggett/.config/nixpkgs/home.nix:1:1, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:170:8:
while evaluating 'optionalAttrs' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/attrsets.nix:321:25, called from /Users/tdoggett/.config/nixpkgs/home.nix:5:7:
while evaluating the module argument `pkgs' in "/Users/tdoggett/.config/nixpkgs/home.nix":
infinite recursion encountered, at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:163:28
error: while evaluating the attribute 'activationPackage' at /nix/store/bjm2jgn200z2w6619f5rchv8r2jiwc8v-master.tar.gz/modules/default.nix:43:3:
while evaluating 'showWarnings' at /nix/store/bjm2jgn200z2w6619f5rchv8r2jiwc8v-master.tar.gz/modules/default.nix:16:18, called from /nix/store/bjm2jgn200z2w6619f5rchv8r2jiwc8v-master.tar.gz/modules/default.nix:28:12:
while evaluating 'foldr' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/lists.nix:36:20, called from /nix/store/bjm2jgn200z2w6619f5rchv8r2jiwc8v-master.tar.gz/modules/default.nix:20:7:
while evaluating 'fold'' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/lists.nix:39:15, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/lists.nix:43:8:
while evaluating 'collectFailed' at /nix/store/bjm2jgn200z2w6619f5rchv8r2jiwc8v-master.tar.gz/modules/default.nix:13:19, called from /nix/store/bjm2jgn200z2w6619f5rchv8r2jiwc8v-master.tar.gz/modules/default.nix:30:16:
while evaluating the attribute 'config' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:87:25:
while evaluating 'yieldConfig' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:74:29, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:73:16:
while evaluating 'mergeModules' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:190:26, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:64:17:
while evaluating 'mergeModules'' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:194:36, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:191:5:
while evaluating 'concatMap' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/lists.nix:104:18, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:230:9:
while evaluating 'reverseList' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/lists.nix:281:17, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:64:38:
while evaluating 'filterModules' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:93:31, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:64:51:
while evaluating 'closeModules' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:101:27, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:62:16:
while evaluating anonymous function at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/lists.nix:96:29, called from undefined position:
while evaluating anonymous function at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:103:50, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/lists.nix:96:32:
while evaluating 'unifyModuleSyntax' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:118:34, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:109:11:
while evaluating 'applyIfFunction' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:144:29, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:109:39:
while evaluating anonymous function at /Users/tdoggett/.config/nixpkgs/home.nix:1:1, called from /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:170:8:
while evaluating 'optionalAttrs' at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/attrsets.nix:321:25, called from /Users/tdoggett/.config/nixpkgs/home.nix:5:7:
while evaluating the module argument `pkgs' in "/Users/tdoggett/.config/nixpkgs/home.nix":
infinite recursion encountered, at /nix/store/x2l5avkjg8q195594mpnwysfb7mhzz8c-nixpkgs-18.09pre140156.0e6fb50954b/nixpkgs/lib/modules.nix:163:28
/Users/tdoggett/.nix-profile/bin/home-manager: line 101: /var/folders/y7/373xplz92v198jt_53tgmlr00000gp/T/home-manager-build.sQ0qCwddxn/news-info.sh: No such file or directory

I'm still rather new to Nix so I get that I may have made some obvious scoping mistake, but I can't figure out what it would be.

@rycee
Copy link
Member

rycee commented Oct 4, 2018

I'm traveling and don't have time to think to hard so I will just hazard a guess at a solution. Try changing

{ pkgs, lib, ... }:
{
  programs.home-manager.enable = true;
  programs.home-manager.path = https://github.com/rycee/home-manager/master.tar.gz;
  
  # programs.emacs.<everything_else> = <etc>; # and so on...
} 
// (lib.optionalAttrs pkgs.stdenv.isLinux {
  programs.emacs.enable = true;
}) 
// (lib.optionalAttrs pkgs.stdenv.isDarwin {
  programs.emacs.enable = false;
})

to

{ pkgs, lib, ... }:

lib.mkMerge [
  {
    programs.home-manager.enable = true;
    programs.home-manager.path = https://github.com/rycee/home-manager/master.tar.gz;

    # programs.emacs.<everything_else> = <etc>; # and so on...
  }

  (lib.mkIf pkgs.stdenv.isLinux {
    programs.emacs.enable = true;
  })

  (lib.mkIf pkgs.stdenv.isDarwin {
    programs.emacs.enable = false;
  })
]

I think the use of // and optionalAttrs will prevent the fix point to converge.

@nocoolnametom
Copy link
Author

Safe travels! I still have a lot to learn about Nix it appears: that worked great! Now for me to go and find out why...

Thanks so much! home-manager is amazing and I'm grateful for the work you and everyone else puts into it.

@rycee
Copy link
Member

rycee commented Oct 6, 2018

No worries. Glad you like HM! 😀

@fzakaria
Copy link

I would love to understand why this causes an infinite recursion

 imports = if pkgs.stdenv.isDarwin then [./platforms/darwin.nix]
  else if pkgs.stdenv.isLinux then [./platforms/linux.nix]
  else [];

@rycee
Copy link
Member

rycee commented Jun 16, 2020

@fzakaria The pkgs argument is set in the config section of a module. So to set the pkgs argument the module system needs to evaluate the modules and to evaluate the modules it first need to assemble all modules, including those in the imports attribute.

In other words, to evaluate pkgs you first need to evaluate the if expression and to evaluate the if expression you first need to evaluate pkgs.

Edit: Just to add some suggestion for alternative solution. I would suggest have a home-darwin.nix and a home-linux.nix and symlinking ~/.config/nixpkgs/home.nix to the one suitable for your platform. See https://rycee.gitlab.io/home-manager/index.html#_how_do_set_up_a_configuration_for_multiple_users_machines.

@adithyaov
Copy link

adithyaov commented Sep 24, 2020

Regarding, infinite recursion, what if I want to use functions defined in pkgs.lib to work with attribute sets in home.nix? This leads to infinite recursion and I kind of get the gist of why that happens. Currently what I'm doing is, instead of using pkgs.lib, I'm using lib = import <nixpkgs/lib>.

lib = import <nixpkgs/lib> feels rather natural analogous to importing a library. Please let me know if there is a better way.

@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/optionalattrs-in-module-infinite-recursion-with-config/27876/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants