Skip to content

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

Open
hercules-ci/flake-parts
#251
@datafoo

Description

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

Metadata

Assignees

No one assigned

    Labels

    0.kind: bugSomething is broken6.topic: flakesThe experimental Nix feature6.topic: nixosIssues or PRs affecting NixOS modules, or package usability issues specific to NixOS

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions