Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nix flake enablement for gardenlogin #73

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Files:
go.sum
LATEST
VERSION
flake.lock
Copyright: 2021 SAP SE or an SAP affiliate company and Gardener contributors
License: Apache-2.0

Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ brew install gardener/tap/gardenlogin
# Chocolatey (Windows)
choco install gardenlogin
```
### Install using Nix

Nix with [Flakes](https://nixos.wiki/wiki/Flakes) (prerequisite: [Nix](https://nixos.org/download), the package manager):

```bash
# Nix (macOS, Linux, and Windows)

# development version
nix profile install github:gardener/gardenlogin
# or release <version>
nix profile install github:gardener/gardenlogin/<version>

#check installation
nix profile list | grep gardenlogin

# optionally, open a new shell and verify that cmd completion works
gardenlogin --help
kubectl gardenlogin --help
vasu1124 marked this conversation as resolved.
Show resolved Hide resolved
```

### Install from Github Release

Expand Down
26 changes: 26 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 106 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
SPDX-FileCopyrightText: 2021 SAP SE or an SAP affiliate company and Gardener contributors
vasu1124 marked this conversation as resolved.
Show resolved Hide resolved

SPDX-License-Identifier: Apache-2.0
*/
{
description = "Nix flake for gardenlogin";

inputs = {
# NixPkgs (nixos-23.11)
vasu1124 marked this conversation as resolved.
Show resolved Hide resolved
nixpkgs.url = "nixpkgs/nixos-23.11"; #"github:NixOS/nixpkgs/nixos-23.11";
vasu1124 marked this conversation as resolved.
Show resolved Hide resolved
};

outputs = { self, nixpkgs, ... }:
let
pname = "gardenlogin";

# System types to support.
supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
vasu1124 marked this conversation as resolved.
Show resolved Hide resolved

# Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'.
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;

# Nixpkgs instantiated for supported system types.
nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; });
in
{
# Provide some binary packages for selected system types.
packages = forAllSystems (system:
let
pkgs = nixpkgsFor.${system};
inherit (pkgs) stdenv lib;
in
{
${pname} = pkgs.buildGo121Module rec {
inherit pname self;
version = lib.fileContents ./VERSION;
splitVersion = lib.versions.splitVersion version;
major = if ((lib.elemAt splitVersion 0) == "v") then lib.elemAt splitVersion 1 else lib.elemAt splitVersion 0;
minor = if ((lib.elemAt splitVersion 0) == "v") then lib.elemAt splitVersion 2 else lib.elemAt splitVersion 1;
gitCommit = if (self ? rev) then self.rev else self.dirtyRev;
state = if (self ? rev) then "clean" else "dirty";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
version = lib.fileContents ./VERSION;
splitVersion = lib.versions.splitVersion version;
major = if ((lib.elemAt splitVersion 0) == "v") then lib.elemAt splitVersion 1 else lib.elemAt splitVersion 0;
minor = if ((lib.elemAt splitVersion 0) == "v") then lib.elemAt splitVersion 2 else lib.elemAt splitVersion 1;
gitCommit = if (self ? rev) then self.rev else self.dirtyRev;
state = if (self ? rev) then "clean" else "dirty";
version = lib.fileContents ./VERSION;
splitVersion = lib.versions.splitVersion version;
major = if ((lib.elemAt splitVersion 0) == "v")
then lib.elemAt splitVersion 1
else lib.elemAt splitVersion 0;
minor = if ((lib.elemAt splitVersion 0) == "v")
then lib.elemAt splitVersion 2
else lib.elemAt splitVersion 1;
gitCommit = if (self ? rev)
then self.rev
else self.dirtyRev;
state = if (self ? rev)
then "clean"
else "dirty";

formatted for better readability

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

splitVersion, major, minor can be repalced with lib.versions.major/minor.

As you can see in the syntax highlighting that no longer works, this is not really how nix does things but I think it would work. The idomatic nix way would be something like:

            gitCommit = if (self ? rev) then
              self.rev 
            else
              self.dirtyRev;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lib.versions.major/minor do not work. there is an optional "v" in the VERSION that spoils the function.


# This vendorHash represents a dervative of all go.mod dependancies and needs to be adjusted with every change
vendorHash = "sha256-vmU0WrrEvfAHuWWrT9anZmQN+YNJIvrgjVUufws0X3s=";
vasu1124 marked this conversation as resolved.
Show resolved Hide resolved

src = ./.;

ldflags = [
"-s" "-w"
"-X k8s.io/component-base/version.gitMajor=${major}"
"-X k8s.io/component-base/version.gitMinor=${minor}"
"-X k8s.io/component-base/version.gitVersion=${version}"
"-X k8s.io/component-base/version.gitTreeState=${state}"
"-X k8s.io/component-base/version.gitCommit=${gitCommit}"
"-X k8s.io/component-base/version/verflag.programName=${pname}"
# "-X k8s.io/component-base/version.buildDate=1970-01-01T0:00:00+0000"
vasu1124 marked this conversation as resolved.
Show resolved Hide resolved
];

CGO_ENABLED = 0;

# subPackages = [
# ];
nativeBuildInputs = [ pkgs.installShellFiles ];

postInstall = ''
ln -s $out/bin/${pname} $out/bin/kubectl-${pname}
installShellCompletion --cmd ${pname} --zsh <($out/bin/${pname} completion zsh)
vasu1124 marked this conversation as resolved.
Show resolved Hide resolved
installShellCompletion --cmd ${pname} --bash <($out/bin/${pname} completion bash)
installShellCompletion --cmd ${pname} --fish <($out/bin/${pname} completion fish)
vasu1124 marked this conversation as resolved.
Show resolved Hide resolved
'';

meta = with lib; {
description = "gardenlogin is a kubectl credential plugin for Gardener";
longDescription = ''
gardenlogin is a kubectl credential plugin that facilitates Gardener managed cluster admin authentication.
It is used to generate kubeconfigs for clusters with short-lived certificates, to access the cluster as cluster-admin.
'';
homepage = "https://github.com/gardener/gardenlogin";
license = licenses.asl20;
platforms = supportedSystems;
};
};
});

# Add dependencies that are only needed for development
devShells = forAllSystems (system:
let
pkgs = nixpkgsFor.${system};
in
{
default = pkgs.mkShell {
buildInputs = with pkgs; [
go_1_21
vasu1124 marked this conversation as resolved.
Show resolved Hide resolved
gopls
gotools
go-tools
gnumake
];
};
});

# The default package for 'nix build'
defaultPackage = forAllSystems (system: self.packages.${system}.${pname});
};
}
vasu1124 marked this conversation as resolved.
Show resolved Hide resolved
Loading