Skip to content

Commit

Permalink
Refactor nix to make it easier to import into other projects (grafana…
Browse files Browse the repository at this point in the history
…#7635)

**What this PR does / why we need it**:

This makes it easier for projects that depend on Loki (like enterprise
logs) to more easily import the Loki nix flake in order to use the
overlays and configurations it contains.

It also removes the `build-vars.nix` because it was super annoying
without really getting us much.
  • Loading branch information
trevorwhitney authored Nov 22, 2022
1 parent 20d801a commit 683efa0
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 106 deletions.
129 changes: 66 additions & 63 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,65 +9,74 @@
# Nixpkgs / NixOS version to use.

outputs = { self, nixpkgs, flake-utils }:
let
golangci-lint-overlay = final: prev: {
golangci-lint = prev.callPackage
"${prev.path}/pkgs/development/tools/golangci-lint"
{
buildGoModule = args:
prev.buildGoModule (args // rec {
version = "1.45.2";

src = prev.fetchFromGitHub rec {
owner = "golangci";
repo = "golangci-lint";
rev = "v${version}";
sha256 =
"sha256-Mr45nJbpyzxo0ZPwx22JW2WrjyjI9FPpl+gZ7NIc6WQ=";
};

vendorSha256 =
"sha256-pcbKg1ePN8pObS9EzP3QYjtaty27L9sroKUs/qEPtJo=";

ldflags = [
"-s"
"-w"
"-X main.version=${version}"
"-X main.commit=v${version}"
"-X main.date=19700101-00:00:00"
];
});
};
};

helm-docs-overlay = final: prev: {
helm-docs = prev.callPackage
"${prev.path}/pkgs/applications/networking/cluster/helm-docs"
{
buildGoModule = args:
prev.buildGoModule (args // rec {
version = "1.8.1";

src = prev.fetchFromGitHub {
owner = "norwoodj";
repo = "helm-docs";
rev = "v${version}";
sha256 = "sha256-OpS/CYBb2Ll6ktvEhqkw/bWMSrFa4duidK3Glu8EnPw=";
};

vendorSha256 = "sha256-FpmeOQ8nV+sEVu2+nY9o9aFbCpwSShQUFOmyzwEQ9Pw=";

ldflags = [
"-w"
"-s"
"-X main.version=v${version}"
];
});
};
};

nix = import ./nix { inherit self; };
in
{
overlays = {
golangci-lint = golangci-lint-overlay;
helm-docs = helm-docs-overlay;
default = nix.overlay;
};
} //
flake-utils.lib.eachDefaultSystem (system:
let
golangci-lint-overlay = final: prev: {
golangci-lint = prev.callPackage
"${prev.path}/pkgs/development/tools/golangci-lint"
{
buildGoModule = args:
prev.buildGoModule (args // rec {
version = "1.45.2";

src = prev.fetchFromGitHub rec {
owner = "golangci";
repo = "golangci-lint";
rev = "v${version}";
sha256 =
"sha256-Mr45nJbpyzxo0ZPwx22JW2WrjyjI9FPpl+gZ7NIc6WQ=";
};

vendorSha256 =
"sha256-pcbKg1ePN8pObS9EzP3QYjtaty27L9sroKUs/qEPtJo=";

ldflags = [
"-s"
"-w"
"-X main.version=${version}"
"-X main.commit=v${version}"
"-X main.date=19700101-00:00:00"
];
});
};
};

helm-docs-overlay = final: prev: {
helm-docs = prev.callPackage
"${prev.path}/pkgs/applications/networking/cluster/helm-docs"
{
buildGoModule = args:
prev.buildGoModule (args // rec {
version = "1.8.1";

src = prev.fetchFromGitHub {
owner = "norwoodj";
repo = "helm-docs";
rev = "v${version}";
sha256 = "sha256-OpS/CYBb2Ll6ktvEhqkw/bWMSrFa4duidK3Glu8EnPw=";
};

vendorSha256 = "sha256-FpmeOQ8nV+sEVu2+nY9o9aFbCpwSShQUFOmyzwEQ9Pw=";

ldflags = [
"-w"
"-s"
"-X main.version=v${version}"
];
});
};
};

nix = import ./nix { inherit self nixpkgs system; };

pkgs = import nixpkgs {
inherit system;
Expand Down Expand Up @@ -141,12 +150,6 @@
chart-testing
chart-releaser
];

shellHook = ''
pushd $(git rev-parse --show-toplevel) > /dev/null || exit 1
./nix/generate-build-vars.sh
popd > /dev/null || exit 1
'';
};
});
}
6 changes: 0 additions & 6 deletions nix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,3 @@ To build the repo (including running tests), from the root of the repo you can r
Nix is supported on Linux, MacOS, and Windows (WSL2). Check [here](https://nixos.org/download.html#download-nix) for installation instructions for your specific platform.

You will also need to enable the Flakes feature to use Nix with this repo. See this [wiki](https://nixos.wiki/wiki/Flakes) for instructions on enabling Flakes.

## Dealing with .git

When building a Nix Flake, the source is first copied into the Nix Store. For immutability (and maybe security) reasons, the `.git` folder is not included in the files copied to the Nix Store. As a result, a Flake cannot rely on `git` commands in it's build. This project does, however, rely on `git` commands during the build. The workaround is the `generate-build-vars.sh` script and `build-vars.nix` file in this folder. The former creates the latter, which should not be edited by hand. There is a shell hook that will run this script whenever you drop into a Nix shell using `nix develop`. While not ideal, by dealing with this through a Nix shell hook, there's no need to change the build process for anyone not using nix.

The `gitRevision` in this file is only used when running `nix build` on a dirty git tree. Otherwise the flake's `self.rev` is used. Therefore, it is probably not necessary to commit changes to `build-vars.nix`.
10 changes: 0 additions & 10 deletions nix/build-vars.nix

This file was deleted.

9 changes: 3 additions & 6 deletions nix/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{ self, nixpkgs, system }:
let buildVars = import ./build-vars.nix;
in
{ self }:
{
overlay = final: prev:
let
Expand All @@ -23,9 +21,9 @@ in

imageTag =
if (self ? rev) then
"${buildVars.gitBranch}-${imageTagVersion}"
"${imageTagVersion}"
else
"${buildVars.gitBranch}-${imageTagVersion}-WIP";
"${imageTagVersion}-WIP";

loki-helm-test = prev.callPackage ../production/helm/loki/src/helm-test {
inherit (prev) pkgs lib buildGoModule dockerTools;
Expand All @@ -37,7 +35,6 @@ in

loki = prev.callPackage ./loki.nix {
inherit imageTag;
inherit (buildVars) gitBranch;
version = shortGitRevsion;
pkgs = prev;
};
Expand Down
19 changes: 0 additions & 19 deletions nix/generate-build-vars.sh

This file was deleted.

4 changes: 2 additions & 2 deletions nix/loki.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ pkgs, version, imageTag, gitBranch }:
{ pkgs, version, imageTag }:
pkgs.stdenv.mkDerivation {
inherit version;

Expand Down Expand Up @@ -29,7 +29,7 @@ pkgs.stdenv.mkDerivation {
--replace "SHELL = /usr/bin/env bash -o pipefail" "SHELL = ${bash}/bin/bash -o pipefail" \
--replace "IMAGE_TAG := \$(shell ./tools/image-tag)" "IMAGE_TAG := ${imageTag}" \
--replace "GIT_REVISION := \$(shell git rev-parse --short HEAD)" "GIT_REVISION := ${version}" \
--replace "GIT_BRANCH := \$(shell git rev-parse --abbrev-ref HEAD)" "GIT_BRANCH := ${gitBranch}" \
--replace "GIT_BRANCH := \$(shell git rev-parse --abbrev-ref HEAD)" "GIT_BRANCH := nix" \
'';

buildPhase = ''
Expand Down

0 comments on commit 683efa0

Please sign in to comment.