Simple flakelight module for adding cross-platform modules for darwin and nixos systems.
- Add
flakelight-crossplatformto your flakelight flake like so:
{
inputs = {
flakelight.url = "github:accelbread/flakelight";
flakelight-darwin.url = "github:cmacrae/flakelight-darwin";
flakelight-crossplatform.url = "github:rencire/flakelight-crossplatform";
};
outputs =
{
flakelight,
flakelight-darwin,
flakelight-crossplatform,
...
}@inputs:
flakelight ./. {
inherit inputs;
imports = [
flakelight-darwin.flakelightModules.default
flakelight-crossplatform.flakelightModules.default
];
};
}
- Add the cross-platform nix module(s) in the
flake.nix, or use flakelight's folder autoload behavior and add the modules as individual files undernix/crossplatformModulesfolder.
Note: the interface is the same as nixosModules, except
we replace nixosModule/nixosModules with crossplatformModule/crossplatformModules.
e.g. Folder structure when using flakelight's folder autoload behavior:
.
├── crossplatformModules
│ └── example.nix
├── darwin
│ └── host1
│ ├── configuration.nix
│ └── default.nix
└── nixos
└── host2
├── configuration.nix
└── default.nix
- Import the the cross-platform modules in the darwin and nixos host configurations. e.g.
nix/darwin/host1/default.nix
{ inputs, ... }:
{
system = "aarch64-darwin";
modules = [
inputs.self.crossplatformModules.example
./configuration.nix
];
}
nix/nixos/host2/default.nix
{ inputs, ... }:
{
system = "x86_64-linux";
modules = [
inputs.self.crossplatformModules.example
./configuration.nix
];
}
- Rebuild hosts
darwin-rebuild switch --flake .#host1
nixos-rebuild switch --flake .#host2
In my flakelight dotfiles, I have both darwin and nixos configurations defined for different macs and pcs I own.
Both of these confiurations use modules defined in the folders darwinModules and nixosModules respectively.
However, I noticed that some of the configuration in the those modules can actually be shared between the two different type of systems.
(e.g. settings for nix such as nix.gc.automatic, nix.settings, etc.)
Here's an example to illustrate the problem and potential solutions:
e.g. folder structure with duplicate module-c module:
.
├── flake.nix
└── nix
├── darwin
│ └── host1
│ ├── configuration.nix
│ └── default.nix
├── darwinModules
│ ├── module-a.nix
│ └── module-c.nix
├── nixos
│ └── host2
│ ├── configuration.nix
│ └── default.nix
└── nixosModules
├── module-b.nix
└── module-c.nix
nix/darwin/host1/default.nix
{ inputs, ... }:
{
system = "aarch64-darwin";
modules = [
inputs.self.darwinModules.module-a
inputs.self.darwinModules.module-c
./configuration.nix
];
}
nix/nixos/host2/default.nix
{ inputs, ... }:
{
system = "x86_64-linux";
modules = [
inputs.self.nixosModules.module-b
inputs.self.nixosModules.module-c
./configuration.nix
];
}
See how inputs.self.nixosModules.example is duplicated in both the nixosModules and the darwinModules folder.
First step we can try to share the common module files is to move them to a crossplatformModules folder.
Folder structure with addition of crossplatformModules folder:
.
├── flake.lock
├── flake.nix
└── nix
├── crossplatformModules
│ └── module-c.nix
├── darwin
│ └── host1
│ ├── configuration.nix
│ └── default.nix
├── darwinModules
│ └── module-a.nix
├── nixos
│ └── host2
│ ├── configuration.nix
│ └── default.nix
└── nixosModules
└── module-b.nix
Now, we'll have to import these via relative file paths:
nix/darwin/host1/default.nix
{ inputs, ... }:
{
system = "aarch64-darwin";
modules = [
inputs.self.darwinModules.module-a
../../crossplatformModules/module-c.nix
./configuration.nix
];
}
nix/nixos/host2/default.nix
{ inputs, ... }:
{
system = "x86_64-linux";
modules = [
inputs.self.nixosModules.module-b
../../crossplatformModules/module-c.nix
./configuration.nix
];
}
- Simple, no need for additional tooling
- Have to maintain relative file references
- Not as consistent with pattern of using
inputs.self...for including re-usable modules.
We can refine the solution to support importing with the inputs.self...
convention in our host configurations.
We'll update the flake.nix to include our flakelight-crossplatform module:
{
inputs = {
flakelight.url = "github:accelbread/flakelight";
flakelight-darwin.url = "github:cmacrae/flakelight-darwin";
flakelight-crossplatform.url = "github:rencire/flakelight-crossplatform"; # Added
};
outputs =
{
flakelight,
flakelight-darwin,
flakelight-crossplatform, # Added
...
}@inputs:
flakelight ./. {
inherit inputs;
imports = [
flakelight-darwin.flakelightModules.default
flakelight-crossplatform.flakelightModules.default # Added
];
};
}
Now we can update the host configurations:
nix/darwin/host1/default.nix
{ inputs, ... }:
{
system = "aarch64-darwin";
modules = [
inputs.self.darwinModules.module-a
inputs.self.crossplatformModules.module-c
./configuration.nix
];
}
nix/nixos/host2/default.nix
{ inputs, ... }:
{
system = "x86_64-linux";
modules = [
inputs.self.nixosModules.module-b
inputs.self.crossplatformModules.module-c
./configuration.nix
];
}
- Can follow the
inputs.self...import convention in our host configurations
- Need to add a dependecny on an external flakelight module
Hopefully the above shows why I created this flakelight module. In short, the main motivation is to avoid duplication in my darwin and nixos configurations.