Skip to content

Commit

Permalink
feat(nix): mkSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamHsieh committed Sep 5, 2024
1 parent 990b86b commit 9842f7e
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 72 deletions.
22 changes: 22 additions & 0 deletions config.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
home = {
username = "william";
fullname = "William Hsieh";
email = "wh31110@gmail.com";
dotDir = "dotfiles"; # path relative to $HOME, this example represents `~/dotfiles`
wsl = false;
gui = false;
};

nixos = {
# enable = true;
hostname = "nixos-local";
system = "x86_64-linux";
};

darwin = {
# enable = true;
hostname = "macos-local";
system = "aarch64-darwin";
};
}
42 changes: 21 additions & 21 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 31 additions & 51 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
inputs.nixpkgs.follows = "nixpkgs";
};

nix-darwin = {
darwin = {
url = "github:LnL7/nix-darwin";
inputs.nixpkgs.follows = "nixpkgs";
};
Expand All @@ -42,10 +42,12 @@
};

outputs = inputs @ { self, nixpkgs, home-manager, ... }:
with self.lib;
let
system = "x86_64-linux";
foreachSystem = genAttrs [ "x86_64-linux" "aarch64-darwin" ];

username = (import ./home/config.nix).user;
hostname = (import ./system/config.nix).host;

# https://github.com/nix-community/home-manager/issues/2942#issuecomment-1378627909
pkgs = import nixpkgs {
Expand All @@ -64,7 +66,9 @@
inherit (self) outputs;
in
{
packages.${system}.default = home-manager.defaultPackage.${system};
lib = import ./lib { inherit inputs; } // inputs.nixpkgs.lib;

# packages.${system}.default = home-manager.defaultPackage.${system};

homeConfigurations = {
${username} = home-manager.lib.homeManagerConfiguration {
Expand All @@ -77,36 +81,12 @@
};
};

nixosConfigurations = {
${hostname} = nixpkgs.lib.nixosSystem {
specialArgs = { inherit inputs outputs pkgs; };
modules = [
./system/nixos
home-manager.nixosModules.home-manager
{
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
home-manager.extraSpecialArgs = { inherit inputs outputs; };
home-manager.users.${username} = import ./home;
}
];
};
nixosConfigurations = lib.mkSystem {
type = "nixos";
};

darwinConfigurations = {
${hostname} = inputs.nix-darwin.lib.darwinSystem {
specialArgs = { inherit inputs outputs pkgs; };
modules = [
./system/darwin
home-manager.darwinModules.home-manager
{
home-manager.useGlobalPkgs = true;
# home-manager.useUserPackages = true;
home-manager.extraSpecialArgs = { inherit inputs outputs; };
home-manager.users.${username} = import ./home;
}
];
};
darwinConfigurations = mkSystem {
type = "darwin";
};

# Convenience output that aggregates the outputs for home, nixos, and darwin configurations.
Expand All @@ -124,25 +104,25 @@
(builtins.attrNames inputs.self.homeConfigurations)
(attr: inputs.self.homeConfigurations.${attr}.activationPackage);
in
nixtop // hometop;

checks.${system}.pre-commit-check = inputs.git-hooks.lib.${system}.run {
src = pkgs.lib.cleanSource ./.;

hooks = {
# TODO: treefmt, selene, shellcheck
editorconfig-checker.enable = true;
nixpkgs-fmt.enable = true;
stylua = {
enable = true;
entry = "${pkgs.stylua}/bin/stylua --config-path ./config/nvim/stylua.toml";
};
};
};

devShells.${system}.default = pkgs.mkShell {
inherit (self.checks.${system}.pre-commit-check) shellHook;
buildInputs = self.checks.${system}.pre-commit-check.enabledPackages;
};
nixtop // darwintop // hometop;

# checks.${system}.pre-commit-check = inputs.git-hooks.lib.${system}.run {
# src = pkgs.lib.cleanSource ./.;
#
# hooks = {
# # TODO: treefmt, selene, shellcheck
# editorconfig-checker.enable = true;
# nixpkgs-fmt.enable = true;
# stylua = {
# enable = true;
# entry = "${pkgs.stylua}/bin/stylua --config-path ./config/nvim/stylua.toml";
# };
# };
# };
#
# devShells.${system}.default = pkgs.mkShell {
# inherit (self.checks.${system}.pre-commit-check) shellHook;
# buildInputs = self.checks.${system}.pre-commit-check.enabledPackages;
# };
};
}
54 changes: 54 additions & 0 deletions lib/default.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,62 @@
{ inputs, ... }:
with inputs.nixpkgs.lib;
let
lockfile = builtins.fromJSON (builtins.readFile ../flake.lock);
inherit (inputs.nixpkgs) lib;

dotfiles = import ../config.nix;

foreachSystem = genAttrs [ "x86_64-linux" "aarch64-darwin" ];

pkgsBySystem = foreachSystem (
system:
# https://github.com/nix-community/home-manager/issues/2942#issuecomment-1378627909
import inputs.nixpkgs {
inherit system;
config = {
allowUnfree = true;
allowUnfreePredicate = (_: true);
packageOverrides = pkgs: {
unstable = import inputs.nixpkgs-unstable {
inherit (pkgs) system config;
};
};
};
}
);

in
{
stateVersion = "${builtins.elemAt (lib.splitString "-" lockfile.nodes.home-manager.original.ref) 1}";

mkSystem = { type }:
let
# TODO: assert type is either "darwin" or "nixos"
isDarwin = type == "darwin";
osConfig = ../system/${if isDarwin then "darwin" else "nixos" };
userHMConfig = ../home;

# NixOS vs nix-darwin functionst
systemFunc = if isDarwin then inputs.darwin.lib.darwinSystem else inputs.nixpkgs.lib.nixosSystem;
osModules = if isDarwin then inputs.home-manager.darwinModules else inputs.home-manager.nixosModules;

hostSystem = if isDarwin then dotfiles.darwin else dotfiles.nixos;
pkgs = pkgsBySystem.${hostSystem.system};
in
{
${hostSystem.hostname} = systemFunc
{
specialArgs = { inherit inputs pkgs dotfiles; };
modules = [
osConfig
osModules.home-manager
{
home-manager.useGlobalPkgs = true;
# home-manager.useUserPackages = true;
home-manager.extraSpecialArgs = { inherit inputs dotfiles; };
home-manager.users.${dotfiles.home.username} = import userHMConfig;
}
];
};
};
}

0 comments on commit 9842f7e

Please sign in to comment.