6

I would like to write my nixos-configuration and home-manager-configuration files in a way that they work on both NixOs (linux) and MacOs (darwin).

While some things are configured the same way on both systems (eg git) other only make sense on one of them (like wayland-windowmanagers being only a thing on linux).

Nix-language features if-else-statements, so now all I need is a way to find out what kind of system I am on.

What I am after is something like:

wayland.sway.enable = if (os == "MacOs") then false else true;

Is there a way to find out what system I am on in nix?

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
Kaligule
  • 630
  • 3
  • 15

2 Answers2

4

I (independently) found a similar answer to that one of to @robert-hensing. So here it is for completeness sake:

The following config will install "hello" on MacOs and "tree" on other systems.

{ config, pkgs, ... }:

let
  my_packages = if pkgs.system == "x86_64-darwin"
                then [ pkgs.hello ]
                else [ pkgs.tree ];
in
{
  environment.systemPackages = my_packages;
}

You can find your pkgs.system string by running nix repl '<nixpkgs>' and then just enter system.

Kaligule
  • 630
  • 3
  • 15
2

In a NixOS module, you can do this:

{ config, lib, pkgs, ... }:
{
  wayland.sway.enable = if pkgs.stdenv.isLinux then true else false;

  # or if you want to be more specific
  wayland.sway.enable = if pkgs.system == "x86_64-linux" then true else false;

  # or if you want to use the default otherwise
  # this works for any option type
  wayland.sway.enable = lib.mkIf pkgs.stdenv.isLinux true;
}

However, I prefer to factor the configuration into modules and then only imports the ones I need.

darwin-configuration.nix

{ ... }:
{
  imports = [ ./common-configuration.nix ];

  launchd.something.something = "...";
}

And for NixOS:

configuration.nix

{ ... }:
{
  imports = [ ./common-configuration.nix ];

  wayland.sway.enable = true;
}
Robert Hensing
  • 6,708
  • 18
  • 23