Import based on system (darwin, linux)

I have two systems, one is macOS and one nixos. I would like to share the configuration and make some imports based on the system.

For hostname specific import I am using

{ config, pkgs, lib, ... }:
let 
  sysconfig = (import <nixpkgs/nixos> {}).config;
in
let
  fqdn = "${sysconfig.networking.hostName}";
in
{
  imports = [
    (./machines + ("/" + fqdn + ".nix") )
  ];

...

in my home.nix.

However I would like to perform similar imports based on the system type. What is the best way to do this?

You could use a similar approach using pkgs.system.

you could always inspect stdenv

  let 
   pathToFile = if pkgs.stdenv.isLinux then
     "./location1"
   else
     "./location2";
   someFile = import pathToFile;
  in
   ....
1 Like

My general recommendation for problems like this is to have a different main Nix file for each platform that just imports a Nix file containing the common code.