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

Usage with NixOS Config #10

Closed
shadowrylander opened this issue Oct 13, 2020 · 6 comments
Closed

Usage with NixOS Config #10

shadowrylander opened this issue Oct 13, 2020 · 6 comments

Comments

@shadowrylander
Copy link

Hello!

How would I go about using this as a replacement for my configuration.nix file, if at all possible? I would like to make sure my config is as pure as possible; here is my current attempt at integrating flake-utils with my flake.nix:

{
  description = "shadowrylander";

  inputs = rec {
    home-manager = {
      url = "github:nix-community/home-manager/master";
      # https://github.com/nix-community/home-manager/blob/master/flake.nix#L4
      # HM takes 'nixpkgs' as input
      inputs.nixpkgs.follows = "nixpkgs";
    };
    mach-nix = {
        url = "github:davhau/mach-nix/master";
        # url = "/etc/nixos/extras/mach-nix";
        inputs.nixpkgs.follows = "nixpkgs";
    };
    # impermanence = {
    #   url = "github:nix-community/impermanence";
    #   flake = false;
    # };
    flake-utils = {
        url = "github:numtide/flake-utils";
        inputs.nixpkgs.follows = "nixpkgs";
    };

    t3.url = "github:NixOS/nixpkgs/nixos-20.03";
    t9.url = "github:NixOS/nixpkgs/nixos-20.09";
    unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
    small.url = "github:NixOS/nixpkgs/nixos-unstable-small";
    master.url = "github:NixOS/nixpkgs/master";

    nixpkgs.follows = "master";

    nix = { url = "github:NixOS/nix/master"; };

  };

  outputs = inputs@{ self, nixpkgs, flake-utils, ... }:
    with flake-utils.lib; eachSystem allSystems (system: let
      pkgs = (import nixpkgs {}).legacyPackages.${system};
    in rec {
      packages = flake-utils.lib.flattenTree {
        nixosConfigurations = let
          base = {
            global = {
              modules = with inputs; [
                home-manager.nixosModules.home-manager
                # mach-nix
              ];
            };
          };
          systems = let stdenvs = (import ./shared/global/stdenvs.nix { inherit pkgs nixpkgs; }); in {
            imd = let _imd = stdenvs.imd; in {
              x64 = _imd.x64.system;
              x86 = _imd.x86.system;
            };
            arm = let _arm = stdenvs.arm; in {
              x64 = _arm.x64.system;
              x86 = _arm.x86.system;
            };
          };
          create = { hostname, pkgs ? pkgs }: let
          # create = { hostname, system ? systems.imd.x64 }: let
          # create = { hostname, system ? systems.imd.x64, pkgs ? pkgs }: let
            config = ./. + "/configs/${hostname}.nix";
          in pkgs.lib.nixosSystem {
            inherit system pkgs;
            modules = base.global.modules ++ [ (import config) ];
          };
        in {
          siluam = create { hostname = "siluam"; };
        };
      };
      defaultPackage = packages.nixosConfigurations;
      apps.nixosConfigurations = flake-utils.lib.mkApp { drv = packages.nixosConfigurations; };
      defaultApp = apps.nixosConfigurations;
    }
  );
}

Thank you kindly for the help!

@SomeoneSerge
Copy link

@shadowrylander In advance: pardon me if I misunderstood or oversimplified your problem, I just skimmed through.


Flakes support a dedicated output type for nixos configurations. The wiki page demonstrates the interface:

{ self, ... }@inputs:
{
# ...
  packages."<system>"."<packagename>" = /* ... */;
  nixosConfigurations."<hostname>" = /* ... */ ;
# ...
}

You seem to be putting your nixosSystem into flake's packages output.:

      packages = flake-utils.lib.flattenTree {
        nixosConfigurations = let /* ... */  in nixosSystem /* ... */;
      };

If nixosConfigurations are per-hostname and not per-system, you probably don't even need flake-utils's eachSystem here.

if you want your flake to also provide some packages you probably can merge the output of flake-utils with an attrset that has just nixosConfigurations. That's a slippery slope though, since you're not supposed to be performing computations in your flake.nix

{
  # ...
  outputs = inputs@{ self, nixpkgs, flake-utils, ... }: (
    with flake-utils.lib; eachSystem allSystems (system: let
      pkgs = (import nixpkgs {}).legacyPackages.${system};
    in rec {
      packages = flake-utils.lib.flattenTree {
      /* whatever */
     }
      defaultPackage = packages./* ... */;
    })
  ) // {
    nixosConfigurations.siluam = nixosSystem { /* ... */ };
  };
}

On the side note, for the apps you'd probably need to find the script that applies nixos configuration. An example with home-manager (found somewhere on reddit): https://github.com/newkozlukov/dotfiles/blob/14ad265db18b58ee18198ccdb2612ac344d64676/flake.nix#L24

@shadowrylander
Copy link
Author

Fair enough! However, the packages thing was me trying to (unsuccessfully) build my NixOS configuration for all possible systems; would your method do that? Or do you think it's not possible at all?

@SomeoneSerge
Copy link

SomeoneSerge commented Oct 21, 2020

Idk, you could take defaultSystems list (available in flake-utils) and apply genAttrs. The nixosSystem function has a system argument. As for where to put the output, I wouldn't know. That wouldn't be related to flake-utils

@shadowrylander
Copy link
Author

shadowrylander commented Oct 21, 2020

If I use genAttrs, I wouldn't be able to use the flake without specifying a build target / system configuration, such as by using #configuration, because without modifying the host name to reflect the system, the rebuild command wouldn't be able to automatically detect the correct configuration.

Sorry if this is confusing; basically, I'm worried that, instead of a configuration like hostname.system, similar to how packages are defined with an attribute per system, the configurations would end up like hostname_system, where the latter itself is the hostname of the machine.

@SomeoneSerge
Copy link

@shadowrylander I'm not sure, I think nixos-rebuild assumes it can distinguish machines by their hostnames, each associated with single system. Either way it's not related to flake-utils. Possibly https://discourse.nixos.org/ or https://github.com/NixOS/nix/issues would be more appropriate

@shadowrylander
Copy link
Author

Hmm... Which means the problem is with the usage of nixosConfigurations, not eachSystem... That's fair. Thanks for the help anyway; I think I know a way to build the systems dynamically!

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

2 participants