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

Add ability to use a lib.nixosSystem system configuration (nixosConfigurations.<name>) as a NixOps deployment node configuration (nixopsConfigurations.<deployment>.<name>) #1516

Open
PAI5REECHO opened this issue Apr 24, 2022 · 5 comments

Comments

@PAI5REECHO
Copy link

This way I can use nixos-rebuild .. and/or nixops deploy .. without repeating myself

@PAI5REECHO PAI5REECHO changed the title Add ability to use a lib.nixosSystem object (nixosConfigurations.<name>) in a NixOps deployment (nixopsConfigurations.<deployment>.<name>) Add ability to use a lib.nixosSystem system configuration (nixosConfigurations.<name>) as a NixOps deployment node configuration (nixopsConfigurations.<deployment>.<name>) Apr 24, 2022
@roberth
Copy link
Member

roberth commented Apr 24, 2022

NixOps relies on the ability to add its own modules, containing such data as ip addresses, ssh keys, etc, whereas a nixosConfigurations.x is supposed to be a complete, final configuration.

nixosModules on the other hand represents the more flexible "input" to NixOS; just some modules. This is a lot like configuration.nix, which has always been a module and which you could imports into NixOps if you like.

So if you don't really need nixopsConfiguration, I would recommend to leave them out, but otherwise I would recommend is something like:

nixosModules.x = /* most of your config */;

nixosModules.xPhysical = /* the relevant bits from `nixops show-physical` */;

nixosConfigurations.x = { imports = [ self.nixosModules.x self.nixosModules.xPhysical ]; }

nixopsConfigurations.y = {
  x = { imports=  [ self.nixosModules.x ]; };
};

@pasqui23
Copy link

pasqui23 commented May 15, 2022

I did

{
    nixosConfigurations = builtins.mapAttrs
      (hostName: v: nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        specialArgs = {
          inherit inputs;
          name = hostName;
        };
        modules = [
          self.nixopsConfigurations.defaults
          n
          ({ lib, ... }: {
            options.deployment = lib.mkOption {
              type = lib.types.anything;
              default = { };
            };
            config.networking = { inherit hostName; };
          })
        ];
      })
      self.nixopsConfigurations.nodes;
}

@nixos-discourse
Copy link

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

https://discourse.nixos.org/t/can-i-reuse-nixosconfigurations-foo-in-nixops/25243/1

@nixos-discourse
Copy link

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

https://discourse.nixos.org/t/can-i-reuse-nixosconfigurations-foo-in-nixops/25243/2

@n8henrie
Copy link

I came up with a similar approach with some additional help / explanation from @roberth (thanks again) and posted it in the linked discussion. The key fact I needed to understand was that I can't go back from a fully configured nixosSystem to a module, so I needed to reconfigure my existing nixosConfigurations into nixosModules which could then be used as the basis for a nixosConfiguration or a nixopsConfiguration. The gist of it is:

let 
...
hosts = rec {
    myLocalMachine.system = "x86_64-linux";
    myRemoteMachine.system = "x86_64-linux";
    myLocalDarwinMachine.system = "aarch64-darwin";
}
match_system = regex: (nixpkgs-stable.lib.filterAttrs (_: host: with host; builtins.match regex system != null) hosts);
...
in {
...
nixosModules =
      builtins.mapAttrs
      (hostname: attrs: {inputs, ...}: {
        imports = [
          ({
            config,
            pkgs,
            ...
          }: {nixpkgs.overlays = default-overlays;})
          other-modules
          ./system-configs/${hostname}/configuration.nix
          home-manager.nixosModules.home-manager
          {
            home-manager = {
              useGlobalPkgs = true;
              useUserPackages = true;
              users.n8henrie = import ./home.nix;
            };
          }
        ];
      })
      (match_system ".*-linux$");

nixosConfigurations = builtins.mapAttrs (_: module:
      lib.nixosSystem {
        modules = [module];
      })
    self.nixosModules;

darwinConfigurations =
      builtins.mapAttrs
      (hostname: sys:
        with sys;
          darwin.lib.darwinSystem {
...
})
      (match_system ".*-darwin$");

nixopsConfigurations.default = {
      nixpkgs = nixpkgs-stable;
      network = {
        storage.legacy = {};
      };
      myRemoteMachine = {
        imports = [self.nixosModules.myRemoteMachine];
        deployment = {
          targetHost = "myRemoteMachine.domain.com";
          targetPort = 22;
        };
      };
    };
}

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

No branches or pull requests

5 participants