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?
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;
}