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

Add nix configuration #7020

Merged
merged 16 commits into from
Sep 6, 2022
Merged

Add nix configuration #7020

merged 16 commits into from
Sep 6, 2022

Conversation

trevorwhitney
Copy link
Collaborator

@trevorwhitney trevorwhitney commented Aug 31, 2022

What this PR does / why we need it:

This PR adds nix configuration to the Loki repo to make it easier for people who use nix to integrate building Loki into their existing workflows. For example, I have a direnv hook that uses nix shell to make sure I have the correct version of all dependent binaries (ie. go, golangci-lint, etc) on my $PATH whenever I change into the Loki directory. I find this especially useful for building with CGO enabled as it requires a C compiler with systemd headers to be available on the path.

Furthermore, by having a flake.nix in the root of this repo, it will make it easier for users of the nix package manager to grab a specific version of Loki for their own systems / projects.

This PR does not attempt to replace any existing build processes, this is merely supplemental. All these nix configurations wrap existing Makefile targets, and the addition of these files will have no effect on anyone not using nix.

Please see the README.md included in this PR for more information.

Checklist

  • Documentation added

@trevorwhitney trevorwhitney requested a review from a team as a code owner August 31, 2022 21:46
{
imageTag = "main-5a6d09f";
gitRevision = "5a6d09f83";
gitBranch = "main";
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

in general there probably isn't much need to commit changes to this file, so I decided to have it track main. The gitRevision is only used on dirty git trees, and there's a shell hook that will automatically update these values for your local branch when you run nix develop

Copy link
Contributor

Choose a reason for hiding this comment

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

What is the reasoning to have it in git?

I suspect to still be able to build the derivation when the flake is imported. I would prefer to have a unknown revision instead. As otherwise it is just implying the wrong thing.

Copy link
Collaborator Author

@trevorwhitney trevorwhitney Sep 1, 2022

Choose a reason for hiding this comment

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

so are you suggesting we use either self.rev or unknown (when self.rev is not set, which would mean the local git tree is dirty)?

@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

1 similar comment
@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

Copy link
Contributor

@simonswine simonswine left a comment

Choose a reason for hiding this comment

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

I welcome this initiative. It is quite useful to have this as standardized environments and maybe in the future we could expand this to also form the basis of our CI/release (if helpful)

I think before we merge we should have some CI around that at least enforced the formatting of nix files and run a build.

For some reason in my local environment (on nixos) I have trouble building the loki derivation and I need that patch:

failed to initialize build cache at /homeless-shelter/.cache/go-build: mkdir /homeless-shelter: permission denied
[...]
failed to initialize build cache at /homeless-shelter/.cache/golangci-lint: mkdir /homeless-shelter: permission denied
--- a/nix/loki.nix
+++ b/nix/loki.nix
@@ -30,11 +30,13 @@ pkgs.stdenv.mkDerivation {
   '';

   buildPhase = ''
+    export GOCACHE=$TMPDIR/go-cache
     make clean loki logcli loki-canary promtail
   '';

   doCheck = true;
   checkPhase = ''
+    export GOCACHE=$TMPDIR/go-cache
     make lint test
   '';

{
imageTag = "main-5a6d09f";
gitRevision = "5a6d09f83";
gitBranch = "main";
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the reasoning to have it in git?

I suspect to still be able to build the derivation when the flake is imported. I would prefer to have a unknown revision instead. As otherwise it is just implying the wrong thing.


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
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think I am using the approach lined out in that link, which if I read correctly is to use self.rev for flakes? Maybe I'm misunderstanding if there's another workaround in that article?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ahh, I see, the suggestion is to use (builtins.fetchGit args).rev. That will not work, as the url here would be ./.. or self, which in either case points to the source in the nix store which has already been stripped of git info, so fetchGit will complain that this is not a git repo.

flake.nix Outdated Show resolved Hide resolved
@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

@trevorwhitney
Copy link
Collaborator Author

What is the reasoning to have it in git?

I suspect to still be able to build the derivation when the flake is imported. I would prefer to have a unknown revision > > instead. As otherwise it is just implying the wrong thing.

So, if you import the flake it will not use the value of gitRevision from this file, it will instead use self.rev, which will be correct. However, maybe we could put only gitBranch in this file, and use either self.rev or unknown or dirty as the sha? That would make it so we should never have to commit changes to this file.

@trevorwhitney
Copy link
Collaborator Author

Build is currently broken, waiting on: #7029

@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

3 similar comments
@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

trevorwhitney added a commit that referenced this pull request Sep 2, 2022
**What this PR does / why we need it**:

The build is currently dependent upon you building in an environment with internet connectivity and GCP credentials. This PR fixes the few parts of our code and test that rely on these external dependencies so it can be built in an environment without internet or GCP credentials (such as when building a [nix flake](#7020))


**Which issue(s) this PR fixes**:
Fixes #7028
@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

@trevorwhitney trevorwhitney merged commit a369189 into grafana:main Sep 6, 2022
@trevorwhitney trevorwhitney deleted the add-nix branch September 6, 2022 17:04
lxwzy pushed a commit to lxwzy/loki that referenced this pull request Nov 7, 2022
**What this PR does / why we need it**:

The build is currently dependent upon you building in an environment with internet connectivity and GCP credentials. This PR fixes the few parts of our code and test that rely on these external dependencies so it can be built in an environment without internet or GCP credentials (such as when building a [nix flake](grafana#7020))


**Which issue(s) this PR fixes**:
Fixes grafana#7028
lxwzy pushed a commit to lxwzy/loki that referenced this pull request Nov 7, 2022
**What this PR does / why we need it**:

This PR adds nix configuration to the Loki repo to make it easier for people who use nix to integrate building Loki into their existing workflows. For example, I have a `direnv` hook that uses nix shell to make sure I have the correct version of all dependent binaries (ie. `go`, `golangci-lint`, etc) on my `$PATH` whenever I change into the Loki directory. I find this especially useful for building with `CGO` enabled as it requires a C compiler with systemd headers to be available on the path.

Furthermore, by having a `flake.nix` in the root of this repo, it will make it easier for users of the nix package manager to grab a specific version of Loki for their own systems / projects.

This PR does _not_ attempt to replace any existing build processes, this is merely supplemental. All these nix configurations wrap existing `Makefile` targets, and the addition of these files will have no effect on anyone not using nix.

Please see the README.md included in this PR for more information.

**Checklist**
- [X] Documentation added

Co-authored-by: Christian Simon <simon@swine.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants