From b9c49d1b40a5c7bf8f844b13761e938d564732d4 Mon Sep 17 00:00:00 2001 From: WilliamHsieh Date: Fri, 30 Aug 2024 15:58:31 +0800 Subject: [PATCH] feat(nix): init darwin config (#62) * feat(nix): initial working nix-darwin config * refactor(nix): restructure nixos config * fixup! feat(nix): initial working nix-darwin config * fixup! feat(nix): initial working nix-darwin config --- flake.lock | 21 ++++++ flake.nix | 26 ++++++- home/alacritty.nix | 1 + home/default.nix | 14 ++-- home/zsh.nix | 13 +++- system/darwin/default.nix | 96 ++++++++++++++++++++++++ system/{ => nixos}/default.nix | 6 +- system/{ => nixos}/hardware.nix | 0 system/{ => nixos}/logiops.nix | 0 system/{ => nixos}/synology-intranet.pem | 0 10 files changed, 164 insertions(+), 13 deletions(-) create mode 100644 system/darwin/default.nix rename system/{ => nixos}/default.nix (95%) rename system/{ => nixos}/hardware.nix (100%) rename system/{ => nixos}/logiops.nix (100%) rename system/{ => nixos}/synology-intranet.pem (100%) diff --git a/flake.lock b/flake.lock index 2a918b16..accc270f 100644 --- a/flake.lock +++ b/flake.lock @@ -96,6 +96,26 @@ "type": "github" } }, + "nix-darwin": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1724561770, + "narHash": "sha256-zv8C9RNa86CIpyHwPIVO/k+5TfM8ZbjGwOOpTe1grls=", + "owner": "LnL7", + "repo": "nix-darwin", + "rev": "ac5694a0b855a981e81b4d9f14052e3ff46ca39e", + "type": "github" + }, + "original": { + "owner": "LnL7", + "repo": "nix-darwin", + "type": "github" + } + }, "nix-index-database": { "inputs": { "nixpkgs": [ @@ -169,6 +189,7 @@ "catppuccin": "catppuccin", "git-hooks": "git-hooks", "home-manager": "home-manager", + "nix-darwin": "nix-darwin", "nix-index-database": "nix-index-database", "nixpkgs": "nixpkgs", "nixpkgs-unstable": "nixpkgs-unstable" diff --git a/flake.nix b/flake.nix index 7713c2a4..c09c3807 100644 --- a/flake.nix +++ b/flake.nix @@ -25,6 +25,11 @@ inputs.nixpkgs.follows = "nixpkgs"; }; + nix-darwin = { + url = "github:LnL7/nix-darwin"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + git-hooks = { url = "github:cachix/git-hooks.nix"; inputs.nixpkgs.follows = "nixpkgs"; @@ -76,7 +81,7 @@ ${hostname} = nixpkgs.lib.nixosSystem { specialArgs = { inherit inputs outputs pkgs; }; modules = [ - ./system + ./system/nixos home-manager.nixosModules.home-manager { home-manager.useGlobalPkgs = true; @@ -88,6 +93,22 @@ }; }; + 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; + } + ]; + }; + }; + # Convenience output that aggregates the outputs for home, nixos, and darwin configurations. # Instead of calling `nix build .#nixosConfigurations.{host}.config.system.build.toplevel`, # now it's simply `nix build .#top.{host}` or `nix build .#top.{user}` @@ -96,6 +117,9 @@ nixtop = nixpkgs.lib.genAttrs (builtins.attrNames inputs.self.nixosConfigurations) (attr: inputs.self.nixosConfigurations.${attr}.config.system.build.toplevel); + darwintop = nixpkgs.lib.genAttrs + (builtins.attrNames inputs.self.darwinConfigurations) + (attr: inputs.self.darwinConfigurations.${attr}.system); hometop = nixpkgs.lib.genAttrs (builtins.attrNames inputs.self.homeConfigurations) (attr: inputs.self.homeConfigurations.${attr}.activationPackage); diff --git a/home/alacritty.nix b/home/alacritty.nix index c3ec6b51..355b92c5 100644 --- a/home/alacritty.nix +++ b/home/alacritty.nix @@ -9,6 +9,7 @@ env = { TERM = "xterm-256color"; LANG = "C.UTF-8"; + CC = "gcc"; }; font = { normal.family = "MesloLGMDZ Nerd Font Mono"; diff --git a/home/default.nix b/home/default.nix index 6dc4265f..1ba91fa9 100644 --- a/home/default.nix +++ b/home/default.nix @@ -54,7 +54,7 @@ in less procps zlib - glibc #ldd, iconv, ... + iconv wget curl @@ -68,8 +68,6 @@ in # images viu - # fast and feature rich - qimgv # export QT_XCB_GL_INTEGRATION=none # super fast feh @@ -99,7 +97,6 @@ in # network httpie socat - netcat-openbsd # only the bsd version support `-k` # fun sl @@ -115,7 +112,10 @@ in hello-unfree #test unfree packages nurl #generate nix fetcher call from repo cloc - ]; + ] ++ (pkgs.lib.optionals pkgs.stdenv.isLinux [ + qimgv # export QT_XCB_GL_INTEGRATION=none + netcat-openbsd # only the bsd version support `-k` + ]); sessionVariables = rec { NIX_PATH = "nixpkgs=${inputs.nixpkgs}"; @@ -126,6 +126,10 @@ in VISUAL = EDITOR; MANPAGER = "nvim +Man!"; LESSUTFCHARDEF = "E000-F8FF:p,F0000-FFFFD:p,100000-10FFFD:p"; # HACK: https://github.com/sharkdp/bat/issues/2578 + + # NOTE: https://github.com/NixOS/nixpkgs/issues/206242 + # LIBRARY_PATH = "${pkgs.iconv}/lib"; + LIBRARY_PATH = "${config.home.profileDirectory}/lib"; }; }; diff --git a/home/zsh.nix b/home/zsh.nix index 6b9eb9fd..9643abcf 100644 --- a/home/zsh.nix +++ b/home/zsh.nix @@ -2,9 +2,7 @@ let cfg = import ./config.nix; dotfilesDir = "${config.home.homeDirectory}/${cfg.repo-path}"; -in -{ - home.shellAliases = { + aliases = { ls = "eza --group-directories-first"; l = "ls -l"; la = "ls -lag --icons=auto"; @@ -19,11 +17,18 @@ in cp = "cp -i"; rm = "trash"; - visudo = "${pkgs.sudo}/bin/visudo"; sudo = ''sudo -E env "PATH=$PATH" ''; pythonServer = "python3 -m http.server"; cpcmd = "fc -ln -1 | awk '{$1=$1}1' | tee /dev/fd/2 | yank"; }; +in +{ + home.shellAliases = pkgs.lib.mkMerge [ + aliases + (pkgs.lib.mkIf pkgs.stdenv.isLinux { + visudo = "${pkgs.sudo}/bin/visudo"; + }) + ]; home.packages = [ pkgs.zsh-forgit diff --git a/system/darwin/default.nix b/system/darwin/default.nix new file mode 100644 index 00000000..6b51b0b6 --- /dev/null +++ b/system/darwin/default.nix @@ -0,0 +1,96 @@ +{ inputs, config, pkgs, ... }: +let + username = (import ../../home/config.nix).user; +in +{ + environment.systemPackages = + [ + pkgs.spotify + pkgs.discord + pkgs.xquartz + ]; + + nixpkgs.hostPlatform = "aarch64-darwin"; + + networking.hostName = (import ../config.nix).host; + + services.karabiner-elements.enable = true; + + users.users."${username}" = { + description = username; + home = "/Users/${username}"; + shell = pkgs.zsh; + }; + + # for login shell + programs.zsh.enable = true; # default shell on catalina + + homebrew = { + enable = true; + casks = [ + "google-chrome" + "google-drive" + "raycast" + "rectangle" + "logi-options+" + "vmware-fusion" + "hammerspoon" + "skype" + "arc" + ]; + }; + + fonts.packages = with pkgs; [ + (nerdfonts.override { + fonts = [ + "Iosevka" + "JetBrainsMono" + "CodeNewRoman" + "Meslo" + "FiraCode" + "DroidSansMono" + ]; + }) + ]; + + system = { + defaults = { + NSGlobalDomain = { + AppleInterfaceStyle = "Dark"; + NSAutomaticCapitalizationEnabled = false; + "com.apple.keyboard.fnState" = true; + }; + dock = { + largesize = 100; + magnification = true; + mru-spaces = false; + persistent-others = [ + "/Users/${username}/Documents" + "/Users/${username}/Downloads" + ]; + }; + trackpad = { + Dragging = true; + }; + finder.FXPreferredViewStyle = "icnv"; + # universalaccess = { + # closeViewScrollWheelToggle = true; + # closeViewZoomFollowsFocus = true; + # }; + }; + + # how to only apply internal keyboard? + keyboard = { + enableKeyMapping = true; + # swapLeftCommandAndLeftAlt = true; + # swapLeftCtrlAndFn = true; + }; + }; + + # Auto upgrade nix package and the daemon service. + services.nix-daemon.enable = true; + + # Used for backwards compatibility, please read the changelog before changing. + # $ darwin-rebuild changelog + system.stateVersion = 4; +} diff --git a/system/default.nix b/system/nixos/default.nix similarity index 95% rename from system/default.nix rename to system/nixos/default.nix index c0be83d5..bfa5ea39 100644 --- a/system/default.nix +++ b/system/nixos/default.nix @@ -19,7 +19,7 @@ }; }; - networking.hostName = (import ./config.nix).host; + networking.hostName = (import ../config.nix).host; # networking.wireless.enable = true; # Enables wireless support via wpa_supplicant. # Configure network proxy if necessary @@ -95,7 +95,7 @@ # Define a user account. Don't forget to set a password with ‘passwd’. users.users.william = { isNormalUser = true; - description = (import ../home/config.nix).name; + description = (import ../../home/config.nix).name; extraGroups = [ "networkmanager" "wheel" ]; shell = pkgs.zsh; packages = with pkgs; [ @@ -161,5 +161,5 @@ # Or disable the firewall altogether. # networking.firewall.enable = false; - system.stateVersion = (import ../lib { inherit inputs; }).stateVersion; + system.stateVersion = (import ../../lib { inherit inputs; }).stateVersion; } diff --git a/system/hardware.nix b/system/nixos/hardware.nix similarity index 100% rename from system/hardware.nix rename to system/nixos/hardware.nix diff --git a/system/logiops.nix b/system/nixos/logiops.nix similarity index 100% rename from system/logiops.nix rename to system/nixos/logiops.nix diff --git a/system/synology-intranet.pem b/system/nixos/synology-intranet.pem similarity index 100% rename from system/synology-intranet.pem rename to system/nixos/synology-intranet.pem