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

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

Open
datafoo opened this issue Nov 7, 2022 · 7 comments · May be fixed by hercules-ci/flake-parts#251
Open

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

datafoo opened this issue Nov 7, 2022 · 7 comments · May be fixed by hercules-ci/flake-parts#251
Labels
0.kind: bug Something is broken 6.topic: flakes The experimental Nix feature 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS

Comments

@datafoo
Copy link
Contributor

datafoo commented Nov 7, 2022

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

  • A import B
  • A import C
  • B import D
  • C import D

nix-env --version output

nix-env (Nix) 2.11.0

@SuperSandro2000
Copy link
Member

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
Copy link
Contributor Author

datafoo commented Nov 7, 2022

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

@jcollie
Copy link
Contributor

jcollie commented Apr 8, 2023

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

This comment was marked as spam.

denbeigh2000 referenced this issue in denbeigh2000/.dotfiles Aug 26, 2023
including them twice causes conflicting declaration errors
maybe https://github.com/NixOS/nix/issues/7270(?)
ngkz referenced this issue in ngkz/dotfiles Jan 15, 2024
@TheColorman
Copy link
Contributor

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
Copy link
Member

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

@datafoo
Copy link
Contributor Author

datafoo commented Sep 4, 2024

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

@fricklerhandwerk fricklerhandwerk transferred this issue from NixOS/nix Sep 7, 2024
@tomodachi94 tomodachi94 added 0.kind: bug Something is broken 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: flakes The experimental Nix feature labels Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.kind: bug Something is broken 6.topic: flakes The experimental Nix feature 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants