From 60964a91e2ba503517e5b5dec46bc2752d6dd73b Mon Sep 17 00:00:00 2001 From: Ahmad Ansori Palembani Date: Sat, 2 Nov 2024 12:11:48 +0700 Subject: [PATCH] refactor(nix/darwin): Include modified brew-nix overlay This one should work on nix v2.18.x since v2.19.x is reported to have regressions --- .config/nix/flake.lock | 36 +------ .config/nix/flake.nix | 11 +- .config/nix/overlays/darwin/brew.nix | 103 +++++++++++++++++++ .config/nix/systems/darwin/configuration.nix | 5 +- .config/nix/systems/darwin/default.nix | 22 ++-- 5 files changed, 128 insertions(+), 49 deletions(-) create mode 100644 .config/nix/overlays/darwin/brew.nix diff --git a/.config/nix/flake.lock b/.config/nix/flake.lock index 2cb756e..8fb790b 100644 --- a/.config/nix/flake.lock +++ b/.config/nix/flake.lock @@ -16,34 +16,6 @@ "type": "github" } }, - "brew-nix": { - "inputs": { - "brew-api": [ - "brew-api" - ], - "flake-utils": "flake-utils", - "nix-darwin": [ - "nix-darwin" - ], - "nixpkgs": [ - "nixpkgs-stable" - ] - }, - "locked": { - "lastModified": 1724407874, - "narHash": "sha256-3nw5O0wLkeTeBXJTyY2pZ57JaRtouLva1a6nVx2syLU=", - "owner": "BatteredBunny", - "repo": "brew-nix", - "rev": "d3d3a2666e5330e83b6a64b1eb3e1e9380f6da9b", - "type": "github" - }, - "original": { - "owner": "BatteredBunny", - "repo": "brew-nix", - "rev": "d3d3a2666e5330e83b6a64b1eb3e1e9380f6da9b", - "type": "github" - } - }, "crane": { "locked": { "lastModified": 1727974419, @@ -121,11 +93,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1710146030, - "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "lastModified": 1726560853, + "narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=", "owner": "numtide", "repo": "flake-utils", - "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a", "type": "github" }, "original": { @@ -312,8 +284,8 @@ "root": { "inputs": { "brew-api": "brew-api", - "brew-nix": "brew-nix", "firefox-darwin": "firefox-darwin", + "flake-utils": "flake-utils", "home-manager": "home-manager", "nix-darwin": "nix-darwin", "nixpkgs-stable": "nixpkgs-stable", diff --git a/.config/nix/flake.nix b/.config/nix/flake.nix index 6bae42b..d8130a9 100644 --- a/.config/nix/flake.nix +++ b/.config/nix/flake.nix @@ -1,7 +1,7 @@ { description = "null2264's Nix Setup"; - outputs = inputs@{ self, nix-darwin, nixpkgs-stable, nixpkgs-unstable, home-manager, system-manager, ... }: + outputs = inputs@{ self, nix-darwin, brew-api, nixpkgs-stable, nixpkgs-unstable, home-manager, system-manager, ... }: let vars = { floorp = { @@ -14,7 +14,7 @@ darwinConfigurations = ( import ./systems/darwin { inherit (nixpkgs-unstable) lib; - inherit inputs nixpkgs-stable nixpkgs-unstable nix-darwin home-manager vars; + inherit inputs nixpkgs-stable nixpkgs-unstable nix-darwin brew-api home-manager vars; } ); @@ -36,6 +36,7 @@ inputs = { nixpkgs-stable.url = "github:NixOS/nixpkgs/release-24.05"; nixpkgs-unstable.url = "github:NixOS/nixpkgs/2d2a9ddbe3f2c00747398f3dc9b05f7f2ebb0f53"; + flake-utils.url = "github:numtide/flake-utils"; nix-darwin = { url = "github:LnL7/nix-darwin/470f87c1827b51169ed4f91cdbdfd48417bfff3d"; @@ -45,12 +46,6 @@ url = "github:bandithedoge/nixpkgs-firefox-darwin"; inputs.nixpkgs.follows = "nixpkgs-stable"; }; - brew-nix = { - url = "github:BatteredBunny/brew-nix/d3d3a2666e5330e83b6a64b1eb3e1e9380f6da9b"; - inputs.nix-darwin.follows = "nix-darwin"; - inputs.brew-api.follows = "brew-api"; - inputs.nixpkgs.follows = "nixpkgs-stable"; - }; brew-api = { url = "github:BatteredBunny/brew-api"; flake = false; diff --git a/.config/nix/overlays/darwin/brew.nix b/.config/nix/overlays/darwin/brew.nix new file mode 100644 index 0000000..c981ece --- /dev/null +++ b/.config/nix/overlays/darwin/brew.nix @@ -0,0 +1,103 @@ +# Modified version of brew-nix/casks.nix +# +# REF: https://github.com/BatteredBunny/brew-nix/blob/9dfab294afea5029922bbc59a10ae487c7318d59/casks.nix +{ system, nixpkgs, brew-api, ... }: +let + pkgs = import nixpkgs { + inherit system; + }; + + hasBinary = cask: pkgs.lib.hasAttr "binary" (getArtifacts cask); + hasApp = cask: pkgs.lib.hasAttr "app" (getArtifacts cask); + hasPkg = cask: pkgs.lib.hasAttr "pkg" (getArtifacts cask); + + getName = cask: builtins.elemAt cask.name 0; + + getBinary = cask: builtins.elemAt (getArtifacts cask).binary 0; + getApp = cask: builtins.elemAt (getArtifacts cask).app 0; + getArtifacts = cask: pkgs.lib.mergeAttrsList cask.artifacts; + + caskToDerivation = cask: pkgs.stdenv.mkDerivation rec { + pname = cask.token; + version = cask.version; + + # FIXME: support variants + src = pkgs.fetchurl { + url = cask.url; + sha256 = pkgs.lib.optionalString (cask.sha256 != "no_check") cask.sha256; + }; + + nativeBuildInputs = with pkgs; [ + undmg + unzip + gzip + _7zz + makeWrapper + ] ++ pkgs.lib.optional (hasPkg cask) (with pkgs; [ + xar + cpio + ]); + + unpackPhase = + if (hasPkg cask) then '' + xar -xf $src + for pkg in $(cat Distribution | grep -oE "#.+\.pkg" | sed -e "s/^#//" -e "s/$/\/Payload/"); do + zcat $pkg | cpio -i + done + '' else if (hasApp cask) then '' + undmg $src || 7zz x -snld $src + '' else if (hasBinary cask) then '' + if [ "$(file --mime-type -b "$src")" == "application/gzip" ]; then + gunzip $src -c > ${getBinary cask} + elif [ "$(file --mime-type -b "$src")" == "application/x-mach-binary" ]; then + cp $src ${getBinary cask} + fi + '' else ""; + + sourceRoot = pkgs.lib.optionalString (hasApp cask) (getApp cask); + + installPhase = + if (hasPkg cask) then '' + mkdir -p $out/Applications + cp -R Applications/* $out/Applications/ + + if [ -d "Resources" ]; then + mkdir -p $out/Resources + cp -R Resources/* $out/Resources/ + fi + + if [ -d "Library" ]; then + mkdir -p $out/Library + cp -R Library/* $out/Library/ + fi + '' else if (hasApp cask) then '' + mkdir -p "$out/Applications/${sourceRoot}" + cp -R . "$out/Applications/${sourceRoot}" + + if [[ -e "$out/Applications/${sourceRoot}/Contents/MacOS/${getName cask}" ]]; then + makeWrapper "$out/Applications/${sourceRoot}/Contents/MacOS/${getName cask}" $out/bin/${cask.token} + elif [[ -e "$out/Applications/${sourceRoot}/Contents/MacOS/${pkgs.lib.removeSuffix ".app" sourceRoot}" ]]; then + makeWrapper "$out/Applications/${sourceRoot}/Contents/MacOS/${pkgs.lib.removeSuffix ".app" sourceRoot}" $out/bin/${cask.token} + fi + '' else if (hasBinary cask && !hasApp cask) then '' + mkdir -p $out/bin + cp -R ./* $out/bin + '' else ""; + + meta = { + homepage = cask.homepage; + description = cask.desc; + platforms = pkgs.lib.platforms.darwin; + mainProgram = if (hasBinary cask && !hasApp cask) then (getBinary cask) else (cask.token); + }; + }; + + casks = builtins.fromJSON (builtins.readFile (brew-api + "/cask.json")); +in final: prev: { + casks = builtins.listToAttrs (builtins.map + (cask: { + name = cask.token; + value = caskToDerivation cask; + }) + casks); +} diff --git a/.config/nix/systems/darwin/configuration.nix b/.config/nix/systems/darwin/configuration.nix index 57c2cb7..96628e8 100644 --- a/.config/nix/systems/darwin/configuration.nix +++ b/.config/nix/systems/darwin/configuration.nix @@ -32,7 +32,8 @@ pkgs.pinentry_mac pkgs.iina pkgs.floorp-bin - pkgs.brewCasks.zotero + + pkgs.casks.zotero pkgs.lf pkgs.yazi # lf replacement, need further testing @@ -41,7 +42,7 @@ # Auto upgrade nix package and the daemon service. services.nix-daemon.enable = true; nix = { - package = pkgs.nixVersions.nix_2_19; # brew-nix requires nix v2.19 + package = pkgs.nix; settings.experimental-features = "nix-command flakes"; # stopping nix from crying about using experimental features flakes and nix-command }; diff --git a/.config/nix/systems/darwin/default.nix b/.config/nix/systems/darwin/default.nix index 15f4c3a..f50a262 100644 --- a/.config/nix/systems/darwin/default.nix +++ b/.config/nix/systems/darwin/default.nix @@ -1,4 +1,4 @@ -{ inputs, nixpkgs-stable, nixpkgs-unstable, nix-darwin, vars, ... }: +{ inputs, nixpkgs-stable, nixpkgs-unstable, nix-darwin, brew-api, vars, ... }: let mkCommon = import ../../lib/mkCommon.nix; @@ -13,18 +13,22 @@ in # Lenovo ThinkPad L460; Intel Core i5-6300U "ThiccBook-Pro" = let + system = "x86_64-darwin"; inherit ( mkSystem { - arch = "x86_64-darwin"; + arch = system; stable = nixpkgs-stable; unstable = nixpkgs-unstable; extraOverlays = [ inputs.firefox-darwin.overlay - inputs.brew-nix.overlays.default + (import ../../overlays/darwin/brew.nix { + inherit system brew-api; + nixpkgs = nixpkgs-stable; + }) (import ../../overlays/darwin/inkscape.nix) ]; } - ) system pkgs pkgs-unstable; + ) pkgs pkgs-unstable; common = (mkCommon { inherit pkgs pkgs-unstable; }); in nix-darwin.lib.darwinSystem { @@ -38,18 +42,22 @@ in # Imaginary M1, just for reference MacBookProM1 = let + system = "aarch64-darwin"; inherit ( mkSystem { - arch = "aarch64-darwin"; + arch = system; stable = nixpkgs-stable; unstable = nixpkgs-unstable; extraOverlays = [ inputs.firefox-darwin.overlay - inputs.brew-nix.overlays.default + (import ../../overlays/darwin/brew.nix { + inherit system brew-api; + nixpkgs = nixpkgs-stable; + }) (import ../../overlays/darwin/inkscape.nix) ]; } - ) system pkgs pkgs-unstable; + ) pkgs pkgs-unstable; common = (mkCommon { inherit pkgs pkgs-unstable; }); in nix-darwin.lib.darwinSystem {