Skip to content

Commit f693470

Browse files
authored
fix: fix nix build error (#1280)
1 parent 1c8ff7b commit f693470

File tree

4 files changed

+85
-68
lines changed

4 files changed

+85
-68
lines changed

default.nix

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ buildGoApplication {
1818
version = "1.8.0";
1919
pwd = ./.;
2020
src = ./.;
21-
# spec go version manually bcs
22-
# https://github.com/nix-community/gomod2nix/blob/30e3c3a9ec4ac8453282ca7f67fca9e1da12c3e6/builder/default.nix#L130
23-
# do not work
24-
go = pkgs.go_1_20;
21+
go = pkgs.go_1_24;
22+
preBuild = ''
23+
go generate main.go
24+
'';
2525
modules = ./gomod2nix.toml;
2626
}

flake.lock

Lines changed: 7 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 70 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
description = "基于 ZeroBot 的 OneBot 插件";
33

4-
inputs.nixpkgs-with-go_1_20.url = "github:NixOS/nixpkgs/33c51330782cb486764eb598d5907b43dc87b4c2";
54
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
65
inputs.flake-utils.url = "github:numtide/flake-utils";
76
inputs.gomod2nix.url = "github:nix-community/gomod2nix";
@@ -11,53 +10,85 @@
1110
outputs = {
1211
self,
1312
nixpkgs,
14-
nixpkgs-with-go_1_20,
1513
flake-utils,
1614
gomod2nix,
17-
...
18-
} @ inputs: let
19-
allSystems = flake-utils.lib.allSystems;
20-
in (
21-
flake-utils.lib.eachSystem allSystems
22-
(system: let
23-
old-nixpkgs = nixpkgs-with-go_1_20.legacyPackages.${system};
24-
pkgs = import nixpkgs {
25-
inherit system;
15+
}: (flake-utils.lib.eachDefaultSystem (
16+
system: let
17+
pkgs = nixpkgs.legacyPackages.${system};
2618

27-
overlays = [
28-
(_: _: {
29-
go_1_20 = old-nixpkgs.go_1_20;
30-
})
19+
callPackage = pkgs.callPackage;
20+
# Simple test check added to nix flake check
21+
go-test = pkgs.stdenvNoCC.mkDerivation {
22+
name = "go-test";
23+
dontBuild = true;
24+
src = ./.;
25+
doCheck = true;
26+
nativeBuildInputs = with pkgs; [
27+
go
28+
writableTmpDirAsHomeHook
3129
];
30+
checkPhase = ''
31+
go test -v ./...
32+
'';
33+
installPhase = ''
34+
mkdir "$out"
35+
'';
3236
};
33-
34-
# The current default sdk for macOS fails to compile go projects, so we use a newer one for now.
35-
# This has no effect on other platforms.
36-
callPackage = pkgs.darwin.apple_sdk_11_0.callPackage or pkgs.callPackage;
37-
in {
38-
# doCheck will fail at write files
39-
packages = rec {
40-
ZeroBot-Plugin = (callPackage ./. (inputs
41-
// {
42-
inherit (gomod2nix.legacyPackages.${system}) buildGoApplication;
43-
}))
44-
.overrideAttrs (_: {doCheck = false;});
45-
46-
default = ZeroBot-Plugin;
47-
48-
docker_builder = pkgs.dockerTools.buildLayeredImage {
49-
name = "ZeroBot-Plugin";
50-
tag = "latest";
51-
contents = [
52-
self.packages.${system}.ZeroBot-Plugin
53-
pkgs.cacert
54-
];
37+
# Simple lint check added to nix flake check
38+
go-lint = pkgs.stdenvNoCC.mkDerivation {
39+
name = "go-lint";
40+
dontBuild = true;
41+
src = ./.;
42+
doCheck = true;
43+
nativeBuildInputs = with pkgs; [
44+
golangci-lint
45+
go
46+
writableTmpDirAsHomeHook
47+
];
48+
checkPhase = ''
49+
golangci-lint run
50+
'';
51+
installPhase = ''
52+
mkdir "$out"
53+
'';
54+
};
55+
# doCheck will fail at download files
56+
ZeroBot-Plugin = (callPackage ./. {
57+
inherit (gomod2nix.legacyPackages.${system}) buildGoApplication;
58+
}).overrideAttrs (_: {doCheck = false;});
59+
# Build container layered image, useful overtime to save storage on duplicated layers
60+
containerImage = pkgs.dockerTools.buildLayeredImage {
61+
name = "ZeroBot-Plugin";
62+
tag = "latest";
63+
created = "now";
64+
contents = [
65+
pkgs.cacert
66+
pkgs.openssl
67+
];
68+
config = {
69+
Cmd = ["${ZeroBot-Plugin}/bin/ZeroBot-Plugin"];
5570
};
5671
};
72+
in {
73+
inherit containerImage;
74+
checks = {
75+
inherit go-test go-lint;
76+
};
77+
packages.default = ZeroBot-Plugin;
5778
devShells.default = callPackage ./shell.nix {
5879
inherit (gomod2nix.legacyPackages.${system}) mkGoEnv gomod2nix;
5980
};
81+
# Custom application to build and load container image into the docker daemon
82+
# For now docker is a requirement
83+
apps.build-and-load = {
84+
type = "app";
85+
program = "${pkgs.writeShellScriptBin "build-and-load" ''
86+
nix build .#containerImage.${system}
87+
docker load < result
88+
echo "Container image loaded"
89+
''}/bin/build-and-load";
90+
};
6091
formatter = pkgs.alejandra;
61-
})
62-
);
92+
}
93+
));
6394
}

shell.nix

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
mkGoEnv ? pkgs.mkGoEnv,
1414
gomod2nix ? pkgs.gomod2nix,
1515
}: let
16-
goEnv = mkGoEnv { pwd = ./.; go = pkgs.go_1_20; };
16+
goEnv = mkGoEnv {
17+
pwd = ./.;
18+
go = pkgs.go_1_24;
19+
};
1720
in
1821
pkgs.mkShell {
1922
packages = [

0 commit comments

Comments
 (0)