NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.87k stars 13.94k forks source link

Multiple imports of the same NixOS module not allowed with flake #340361

Open datafoo opened 1 year ago

datafoo commented 1 year ago

Describe the bug

Importing multiple times a given NixOS module exposed by a flake is not allowed. Nix complains about options being already declared.

Steps To Reproduce

  1. Define a flake that expose a NixOS module which declare at least one option: flake.nix:

    {
      outputs = { self }: {
    
        nixosModules.debug = { config, lib, ... }:
        {
          options = {
    
            debug = {
    
              enable = lib.mkOption {
                type = lib.types.bool;
                default = false;
                description = ''
                  Whether to enable debug.
                '';
              };
            };
          };
    
          config = lib.mkIf config.debug.enable {
    
            systemd.globalEnvironment = {
              SYSTEMD_LOG_LEVEL = "debug";
            };
    
          };
        };
    
      };
    }
    
  2. Define another flake that uses the previous flake: flake.nix:

    {
      inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-22.05;
      inputs.debug.url = path:/home/me/flakes/debug;
    
      outputs = inputs@{ self, nixpkgs, ... }: {
    
        nixosConfigurations.mymachine = nixpkgs.lib.nixosSystem {
          system = "x86_64-linux";
          modules =
            [ 
              (import ./configuration.nix)
            ];
          specialArgs = { inherit inputs; };
        };
    
      };
    }

    configuration.nix:

    { config, pkgs, inputs, ... }:
    
    {
      imports =
        [
          ./hardware-configuration.nix
          inputs.debug.nixosModules.debug
          inputs.debug.nixosModules.debug
        ];
    
      # here goes the rest of the config
    }
  3. Build the configuration and notice the error
    $ nixos-rebuild switch --flake .#mymachine
    building the system configuration...
    error: The option `debug.enable' in `/nix/store/da9dyx30lsdgw2x2gnxkc88aq1d9fwik-source/flake.nix' is already declared in `/nix/store/da9dyx30lsdgw2x2gnxkc88aq1d9fwik-source/flake.nix'.
    (use '--show-trace' to show detailed location information)

Expected behavior

It should be allowed to import multiple times a NixOS module that declare options. It already works today unless this module is exposed by a flake.

Importing multiple times into the same module is not very useful. What is useful is to import the same module (i.e.: D) into several modules (i.e.: B, C) which are themselves imported into one final module (i.e.: A):

nix-env --version output

nix-env (Nix) 2.11.0

SuperSandro2000 commented 1 year ago

It should be allowed to import multiple times a NixOS module that declare options. It already works today unless this modules is exposed by a flake.

Where the relative import paths the same before?

datafoo commented 1 year ago

I made a test with a relative import path duplicated and it was working.

jcollie commented 1 year ago

I worked around this by creating a new module E that imports D from a flake. Then I modify B & C to import E instead of importing D directly.

datafoo commented 1 year ago

ping

TheColorman commented 1 month ago

This is quite annoying when you have several modules with the same module dependency, and have situations where you want to import two or more of them.

SuperSandro2000 commented 1 month ago

@fricklerhandwerk the module system is defined in nixpkgs, so we should move zhe issue to there

datafoo commented 1 month ago

@fricklerhandwerk the module system is defined in nixpkgs, so we should move zhe issue to there

Feel free to do so. I do not have the permission to do it myself.