Refactor nix to make it easier to import into other projects (#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.
pull/7695/head
Trevor Whitney 3 years ago committed by GitHub
parent 20d801a09d
commit 683efa0f4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 129
      flake.nix
  2. 6
      nix/README.md
  3. 10
      nix/build-vars.nix
  4. 9
      nix/default.nix
  5. 19
      nix/generate-build-vars.sh
  6. 4
      nix/loki.nix

@ -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;
@ -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
'';
};
});
}

@ -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`.

@ -1,10 +0,0 @@
# DO NOT EDIT, this file is auto-generated by
# generate-build-vars.sh for purposes of getting around
# the dependency our build has on the .git folder, which is
# not copied to the nix store when building.
# Branch should match the branch you're building on, so
# changes to this file should not be comitted to main.
# See the README for more details.
{
gitBranch = "main";
}

@ -1,6 +1,4 @@
{ self, nixpkgs, system }:
let buildVars = import ./build-vars.nix;
in
{ self }:
{
overlay = final: prev:
let
@ -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;
@ -37,7 +35,6 @@ in
loki = prev.callPackage ./loki.nix {
inherit imageTag;
inherit (buildVars) gitBranch;
version = shortGitRevsion;
pkgs = prev;
};

@ -1,19 +0,0 @@
#!/bin/bash
current_dir=$(cd "$(dirname "$0")" && pwd)
root_dir=$(cd "${current_dir}/.." && pwd)
pushd "${root_dir}" >/dev/null || exit 1
cat <<NIX >"${current_dir}/build-vars.nix"
# DO NOT EDIT, this file is auto-generated by
# generate-build-vars.sh for purposes of getting around
# the dependency our build has on the .git folder, which is
# not copied to the nix store when building.
# Branch should match the branch you're building on, so
# changes to this file should not be comitted to main.
# See the README for more details.
{
gitBranch = "$(git rev-parse --abbrev-ref HEAD)";
}
NIX
popd >/dev/null || exit 1

@ -1,4 +1,4 @@
{ pkgs, version, imageTag, gitBranch }:
{ pkgs, version, imageTag }:
pkgs.stdenv.mkDerivation {
inherit version;
@ -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 = ''

Loading…
Cancel
Save