Isolate common nix into lib.nix

This commit is contained in:
Sridhar Ratnakumar 2022-12-03 21:57:30 -05:00
parent 2fdf57226a
commit 08bfaa4e88
6 changed files with 93 additions and 85 deletions

View file

@ -1,32 +0,0 @@
# A rough flake-parts module for activating the system
#
# TODO: Replace with deploy-rs or (new) nixinate
{ self, inputs, ... }:
{
perSystem = { system, pkgs, lib, ... }: {
apps.default =
let
# Create a flake app that wraps the given bash CLI.
bashCmdApp = name: cmd: {
type = "app";
program =
(pkgs.writeShellApplication {
inherit name;
text = ''
set -x
${cmd}
'';
}) + "/bin/${name}";
};
in
if system == "aarch64-darwin" then
bashCmdApp "darwin" ''
${self.darwinConfigurations.default.system}/sw/bin/darwin-rebuild \
switch --flake ${self}#default
''
else
bashCmdApp "linux" ''
${lib.getExe pkgs.nixos-rebuild} --use-remote-sudo switch -j auto
'';
};
}

View file

@ -33,11 +33,11 @@
inputs.flake-parts.lib.mkFlake { inherit (inputs) self; } {
systems = [ "x86_64-linux" "aarch64-darwin" ];
imports = [
./lib.nix
./config.nix
./home
./nixos
./nix-darwin
./activate.nix
];
people = {
@ -68,11 +68,9 @@
];
};
};
# Configurations for my only[^1] macOS machine (using nix-darwin)
#
# [^1]: This is why attr key is 'default'.
# Configurations for my (only) macOS machine (using nix-darwin)
darwinConfigurations = {
default = self.lib-darwin.mkMacosSystem {
default = self.lib.mkMacosSystem {
imports = [
self.darwinModules.default # Defined in nix-darwin/default.nix
./systems/darwin.nix
@ -81,11 +79,12 @@
};
};
perSystem = { pkgs, ... }: {
perSystem = { pkgs, config, ... }: {
devShells.default = pkgs.mkShell {
buildInputs = [ pkgs.nixpkgs-fmt ];
};
formatter = pkgs.nixpkgs-fmt;
apps.default = config.apps.activate;
};
};
}

View file

@ -33,33 +33,5 @@
];
};
};
nixosModules.home-manager = {
imports = [
inputs.home-manager.nixosModules.home-manager
({
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
home-manager.extraSpecialArgs = {
inherit inputs;
system = "x86_64-linux";
flake = { inherit config; };
};
})
];
};
darwinModules.home-manager = {
imports = [
inputs.home-manager.darwinModules.home-manager
({
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
home-manager.extraSpecialArgs = {
inherit inputs;
system = "aarch64-darwin";
flake = { inherit config; };
};
})
];
};
};
}

87
lib.nix Normal file
View file

@ -0,0 +1,87 @@
# Support code for this repo. This module could be made its own external repo.
{ self, inputs, config, ... }:
{
flake = {
# Linux home-manager module
nixosModules.home-manager = {
imports = [
inputs.home-manager.nixosModules.home-manager
({
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
home-manager.extraSpecialArgs = {
inherit inputs;
system = "x86_64-linux";
flake = { inherit config; };
};
})
];
};
# macOS home-manager module
darwinModules.home-manager = {
imports = [
inputs.home-manager.darwinModules.home-manager
({
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
home-manager.extraSpecialArgs = {
inherit inputs;
system = "aarch64-darwin";
flake = { inherit config; };
};
})
];
};
lib = {
mkLinuxSystem = mod: inputs.nixpkgs.lib.nixosSystem rec {
system = "x86_64-linux";
# Arguments to pass to all modules.
specialArgs = {
inherit system inputs;
flake = { inherit config; };
};
modules = [ mod ];
};
mkMacosSystem = mod: inputs.darwin.lib.darwinSystem rec {
system = "aarch64-darwin";
specialArgs = {
inherit inputs system;
flake = { inherit config; };
rosettaPkgs = import inputs.nixpkgs { system = "x86_64-darwin"; };
};
modules = [ mod ];
};
};
};
perSystem = { system, pkgs, lib, ... }: {
# A rough app for activating the system locally.
#
# TODO: Replace with deploy-rs or (new) nixinate
apps.activate =
let
# Create a flake app that wraps the given bash CLI.
bashCmdApp = name: cmd: {
type = "app";
program =
(pkgs.writeShellApplication {
inherit name;
text = ''
set -x
${cmd}
'';
}) + "/bin/${name}";
};
in
if system == "aarch64-darwin" then
bashCmdApp "darwin" ''
${self.darwinConfigurations.default.system}/sw/bin/darwin-rebuild \
switch --flake ${self}#default
''
else
bashCmdApp "linux" ''
${lib.getExe pkgs.nixos-rebuild} --use-remote-sudo switch -j auto
'';
};
}

View file

@ -18,14 +18,6 @@
../nixos/caches
];
};
lib-darwin.mkMacosSystem = mod: inputs.darwin.lib.darwinSystem rec {
system = "aarch64-darwin";
specialArgs = {
inherit inputs system;
flake = { inherit config; };
rosettaPkgs = import inputs.nixpkgs { system = "x86_64-darwin"; };
};
modules = [ mod ];
};
};
}

View file

@ -30,15 +30,5 @@ in
./current-location.nix
];
};
lib.mkLinuxSystem = mod: inputs.nixpkgs.lib.nixosSystem rec {
system = "x86_64-linux";
# Arguments to pass to all modules.
specialArgs = {
inherit system inputs;
flake = { inherit config; };
};
modules = [ mod ];
};
};
}