From f6934709cc0ce34fa6c03fa95df1d5cdd3c443cc Mon Sep 17 00:00:00 2001 From: Nobody6825 <117302867+Nobooooody@users.noreply.github.com> Date: Fri, 30 Jan 2026 23:18:10 +0800 Subject: [PATCH] fix: fix nix build error (#1280) --- default.nix | 8 ++-- flake.lock | 31 ++++----------- flake.nix | 109 +++++++++++++++++++++++++++++++++------------------- shell.nix | 5 ++- 4 files changed, 85 insertions(+), 68 deletions(-) diff --git a/default.nix b/default.nix index 5d8ebfad..1268fc19 100644 --- a/default.nix +++ b/default.nix @@ -18,9 +18,9 @@ buildGoApplication { version = "1.8.0"; pwd = ./.; src = ./.; - # spec go version manually bcs - # https://github.com/nix-community/gomod2nix/blob/30e3c3a9ec4ac8453282ca7f67fca9e1da12c3e6/builder/default.nix#L130 - # do not work - go = pkgs.go_1_20; + go = pkgs.go_1_24; + preBuild = '' + go generate main.go + ''; modules = ./gomod2nix.toml; } diff --git a/flake.lock b/flake.lock index 36b302f6..41acb75c 100644 --- a/flake.lock +++ b/flake.lock @@ -28,11 +28,11 @@ ] }, "locked": { - "lastModified": 1742209644, - "narHash": "sha256-jMy1XqXqD0/tJprEbUmKilTkvbDY/C0ZGSsJJH4TNCE=", + "lastModified": 1767019875, + "narHash": "sha256-NodN+lhWTD59b44Q2bPjE1edINfjfRkQYdZsrxifCeU=", "owner": "nix-community", "repo": "gomod2nix", - "rev": "8f3534eb8f6c5c3fce799376dc3b91bae6b11884", + "rev": "49662a44272806ff785df2990a420edaaca15db4", "type": "github" }, "original": { @@ -43,11 +43,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1745391562, - "narHash": "sha256-sPwcCYuiEopaafePqlG826tBhctuJsLx/mhKKM5Fmjo=", + "lastModified": 1769461804, + "narHash": "sha256-msG8SU5WsBUfVVa/9RPLaymvi5bI8edTavbIq3vRlhI=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "8a2f738d9d1f1d986b5a4cd2fd2061a7127237d7", + "rev": "bfc1b8a4574108ceef22f02bafcf6611380c100d", "type": "github" }, "original": { @@ -57,28 +57,11 @@ "type": "github" } }, - "nixpkgs-with-go_1_20": { - "locked": { - "lastModified": 1710843028, - "narHash": "sha256-CMbK45c4nSkGvayiEHFkGFH+doGPbgo3AWfecd2t1Fk=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "33c51330782cb486764eb598d5907b43dc87b4c2", - "type": "github" - }, - "original": { - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "33c51330782cb486764eb598d5907b43dc87b4c2", - "type": "github" - } - }, "root": { "inputs": { "flake-utils": "flake-utils", "gomod2nix": "gomod2nix", - "nixpkgs": "nixpkgs", - "nixpkgs-with-go_1_20": "nixpkgs-with-go_1_20" + "nixpkgs": "nixpkgs" } }, "systems": { diff --git a/flake.nix b/flake.nix index 21b6eec4..73154a1b 100644 --- a/flake.nix +++ b/flake.nix @@ -1,7 +1,6 @@ { description = "基于 ZeroBot 的 OneBot 插件"; - inputs.nixpkgs-with-go_1_20.url = "github:NixOS/nixpkgs/33c51330782cb486764eb598d5907b43dc87b4c2"; inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; inputs.flake-utils.url = "github:numtide/flake-utils"; inputs.gomod2nix.url = "github:nix-community/gomod2nix"; @@ -11,53 +10,85 @@ outputs = { self, nixpkgs, - nixpkgs-with-go_1_20, flake-utils, gomod2nix, - ... - } @ inputs: let - allSystems = flake-utils.lib.allSystems; - in ( - flake-utils.lib.eachSystem allSystems - (system: let - old-nixpkgs = nixpkgs-with-go_1_20.legacyPackages.${system}; - pkgs = import nixpkgs { - inherit system; + }: (flake-utils.lib.eachDefaultSystem ( + system: let + pkgs = nixpkgs.legacyPackages.${system}; - overlays = [ - (_: _: { - go_1_20 = old-nixpkgs.go_1_20; - }) + callPackage = pkgs.callPackage; + # Simple test check added to nix flake check + go-test = pkgs.stdenvNoCC.mkDerivation { + name = "go-test"; + dontBuild = true; + src = ./.; + doCheck = true; + nativeBuildInputs = with pkgs; [ + go + writableTmpDirAsHomeHook ]; + checkPhase = '' + go test -v ./... + ''; + installPhase = '' + mkdir "$out" + ''; }; - - # The current default sdk for macOS fails to compile go projects, so we use a newer one for now. - # This has no effect on other platforms. - callPackage = pkgs.darwin.apple_sdk_11_0.callPackage or pkgs.callPackage; - in { - # doCheck will fail at write files - packages = rec { - ZeroBot-Plugin = (callPackage ./. (inputs - // { - inherit (gomod2nix.legacyPackages.${system}) buildGoApplication; - })) - .overrideAttrs (_: {doCheck = false;}); - - default = ZeroBot-Plugin; - - docker_builder = pkgs.dockerTools.buildLayeredImage { - name = "ZeroBot-Plugin"; - tag = "latest"; - contents = [ - self.packages.${system}.ZeroBot-Plugin - pkgs.cacert - ]; + # Simple lint check added to nix flake check + go-lint = pkgs.stdenvNoCC.mkDerivation { + name = "go-lint"; + dontBuild = true; + src = ./.; + doCheck = true; + nativeBuildInputs = with pkgs; [ + golangci-lint + go + writableTmpDirAsHomeHook + ]; + checkPhase = '' + golangci-lint run + ''; + installPhase = '' + mkdir "$out" + ''; + }; + # doCheck will fail at download files + ZeroBot-Plugin = (callPackage ./. { + inherit (gomod2nix.legacyPackages.${system}) buildGoApplication; + }).overrideAttrs (_: {doCheck = false;}); + # Build container layered image, useful overtime to save storage on duplicated layers + containerImage = pkgs.dockerTools.buildLayeredImage { + name = "ZeroBot-Plugin"; + tag = "latest"; + created = "now"; + contents = [ + pkgs.cacert + pkgs.openssl + ]; + config = { + Cmd = ["${ZeroBot-Plugin}/bin/ZeroBot-Plugin"]; }; }; + in { + inherit containerImage; + checks = { + inherit go-test go-lint; + }; + packages.default = ZeroBot-Plugin; devShells.default = callPackage ./shell.nix { inherit (gomod2nix.legacyPackages.${system}) mkGoEnv gomod2nix; }; + # Custom application to build and load container image into the docker daemon + # For now docker is a requirement + apps.build-and-load = { + type = "app"; + program = "${pkgs.writeShellScriptBin "build-and-load" '' + nix build .#containerImage.${system} + docker load < result + echo "Container image loaded" + ''}/bin/build-and-load"; + }; formatter = pkgs.alejandra; - }) - ); + } + )); } diff --git a/shell.nix b/shell.nix index 303d0b78..b75fcb58 100644 --- a/shell.nix +++ b/shell.nix @@ -13,7 +13,10 @@ mkGoEnv ? pkgs.mkGoEnv, gomod2nix ? pkgs.gomod2nix, }: let - goEnv = mkGoEnv { pwd = ./.; go = pkgs.go_1_20; }; + goEnv = mkGoEnv { + pwd = ./.; + go = pkgs.go_1_24; + }; in pkgs.mkShell { packages = [