Skip to content

Commit

Permalink
k3s: allow overriding go derivations (NixOS#365896)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusramberg authored Dec 23, 2024
2 parents a7f85ab + 40d4fd0 commit 47bf700
Showing 1 changed file with 94 additions and 85 deletions.
179 changes: 94 additions & 85 deletions pkgs/applications/networking/cluster/k3s/builder.nix
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ lib:
libseccomp,
makeWrapper,
nixosTests,
overrideBundleAttrs ? { }, # An attrSet/function to override the `k3sBundle` derivation.
overrideCniPluginsAttrs ? { }, # An attrSet/function to override the `k3sCNIPlugins` derivation.
overrideContainerdAttrs ? { }, # An attrSet/function to override the `k3sContainerd` derivation.
pkg-config,
pkgsBuildBuild,
procps,
rsync,
runc,
runCommand,
runc,
socat,
sqlite,
stdenv,
Expand Down Expand Up @@ -174,28 +177,30 @@ let
sha256 = k3sRootSha256;
stripRoot = false;
};
k3sCNIPlugins = buildGoModule rec {
pname = "k3s-cni-plugins";
version = k3sCNIVersion;
vendorHash = null;

subPackages = [ "." ];

src = fetchFromGitHub {
owner = "rancher";
repo = "plugins";
rev = "v${version}";
sha256 = k3sCNISha256;
};

postInstall = ''
mv $out/bin/plugins $out/bin/cni
'';

meta = baseMeta // {
description = "CNI plugins, as patched by rancher for k3s";
};
};
k3sCNIPlugins =
(buildGoModule rec {
pname = "k3s-cni-plugins";
version = k3sCNIVersion;
vendorHash = null;

subPackages = [ "." ];

src = fetchFromGitHub {
owner = "rancher";
repo = "plugins";
rev = "v${version}";
sha256 = k3sCNISha256;
};

postInstall = ''
mv $out/bin/plugins $out/bin/cni
'';

meta = baseMeta // {
description = "CNI plugins, as patched by rancher for k3s";
};
}).overrideAttrs
overrideCniPluginsAttrs;
# Grab this separately from a build because it's used by both stages of the
# k3s build.
k3sRepo = fetchgit {
Expand Down Expand Up @@ -261,67 +266,71 @@ let
# derivation when we've built all the binaries, but haven't bundled them in
# with generated bindata yet.

k3sServer = buildGoModule {
pname = "k3s-server";
version = k3sVersion;

src = k3sRepo;
vendorHash = k3sVendorHash;

nativeBuildInputs = [ pkg-config ];
buildInputs = [
libseccomp
sqlite.dev
];

subPackages = [ "cmd/server" ];
ldflags = versionldflags;

tags = [
"ctrd"
"libsqlite3"
"linux"
];

# create the multicall symlinks for k3s
postInstall = ''
mv $out/bin/server $out/bin/k3s
pushd $out
# taken verbatim from https://github.com/k3s-io/k3s/blob/v1.23.3%2Bk3s1/scripts/build#L105-L113
ln -s k3s ./bin/containerd
ln -s k3s ./bin/crictl
ln -s k3s ./bin/ctr
ln -s k3s ./bin/k3s-agent
ln -s k3s ./bin/k3s-certificate
ln -s k3s ./bin/k3s-completion
ln -s k3s ./bin/k3s-etcd-snapshot
ln -s k3s ./bin/k3s-secrets-encrypt
ln -s k3s ./bin/k3s-server
ln -s k3s ./bin/k3s-token
ln -s k3s ./bin/kubectl
popd
'';

meta = baseMeta // {
description = "Various binaries that get packaged into the final k3s binary";
};
};
k3sBundle =
(buildGoModule {
pname = "k3s-bin";
version = k3sVersion;

src = k3sRepo;
vendorHash = k3sVendorHash;

nativeBuildInputs = [ pkg-config ];
buildInputs = [
libseccomp
sqlite.dev
];

subPackages = [ "cmd/server" ];
ldflags = versionldflags;

tags = [
"ctrd"
"libsqlite3"
"linux"
];

# create the multicall symlinks for k3s
postInstall = ''
mv $out/bin/server $out/bin/k3s
pushd $out
# taken verbatim from https://github.com/k3s-io/k3s/blob/v1.23.3%2Bk3s1/scripts/build#L105-L113
ln -s k3s ./bin/containerd
ln -s k3s ./bin/crictl
ln -s k3s ./bin/ctr
ln -s k3s ./bin/k3s-agent
ln -s k3s ./bin/k3s-certificate
ln -s k3s ./bin/k3s-completion
ln -s k3s ./bin/k3s-etcd-snapshot
ln -s k3s ./bin/k3s-secrets-encrypt
ln -s k3s ./bin/k3s-server
ln -s k3s ./bin/k3s-token
ln -s k3s ./bin/kubectl
popd
'';

meta = baseMeta // {
description = "Various binaries that get packaged into the final k3s binary";
};
}).overrideAttrs
overrideBundleAttrs;
# Only used for the shim since
# https://github.com/k3s-io/k3s/blob/v1.27.2%2Bk3s1/scripts/build#L153
k3sContainerd = buildGoModule {
pname = "k3s-containerd";
version = containerdVersion;
src = fetchFromGitHub {
owner = "k3s-io";
repo = "containerd";
rev = "v${containerdVersion}";
sha256 = containerdSha256;
};
vendorHash = null;
buildInputs = [ btrfs-progs ];
subPackages = [ "cmd/containerd-shim-runc-v2" ];
ldflags = versionldflags;
};
k3sContainerd =
(buildGoModule {
pname = "k3s-containerd";
version = containerdVersion;
src = fetchFromGitHub {
owner = "k3s-io";
repo = "containerd";
rev = "v${containerdVersion}";
sha256 = containerdSha256;
};
vendorHash = null;
buildInputs = [ btrfs-progs ];
subPackages = [ "cmd/containerd-shim-runc-v2" ];
ldflags = versionldflags;
}).overrideAttrs
overrideContainerdAttrs;
in
buildGoModule rec {
pname = "k3s";
Expand Down Expand Up @@ -397,7 +406,7 @@ buildGoModule rec {
propagatedBuildInputs = [
k3sCNIPlugins
k3sContainerd
k3sServer
k3sBundle
];

# We override most of buildPhase due to peculiarities in k3s's build.
Expand All @@ -411,7 +420,7 @@ buildGoModule rec {
# copy needed 'go generate' inputs into place
mkdir -p ./bin/aux
rsync -a --no-perms ${k3sServer}/bin/ ./bin/
rsync -a --no-perms ${k3sBundle}/bin/ ./bin/
ln -vsf ${k3sCNIPlugins}/bin/cni ./bin/cni
ln -vsf ${k3sContainerd}/bin/containerd-shim-runc-v2 ./bin
rsync -a --no-perms --chmod u=rwX ${k3sRoot}/etc/ ./etc/
Expand Down Expand Up @@ -463,7 +472,7 @@ buildGoModule rec {
k3sContainerd = k3sContainerd;
k3sRepo = k3sRepo;
k3sRoot = k3sRoot;
k3sServer = k3sServer;
k3sBundle = k3sBundle;
mkTests =
version:
let
Expand Down

0 comments on commit 47bf700

Please sign in to comment.