-
Notifications
You must be signed in to change notification settings - Fork 113
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 a Nix flake #9023
base: main
Are you sure you want to change the base?
Add a Nix flake #9023
Conversation
This adds a fairly rich Nix environment. It provides a shell that includes rustfmt and rust-analyzer, and a Home Manager module for configuring & running Zebra via Nix.
Simple CI for Nix
Hello. I'm currently building and running zebra on nixos and I created my own |
I'm often learning nix as I go, and the same is true with this PR. I started by rebasing this branch onto a more recent That asked me a series of questions, so I started documenting my process and stopped to post this: Security AnalysisHere's a tangent to document development/infrastructure security for the nix system. This could be useful for my own understanding as well as spreading knowledge of the security profile for the rest of the Zebra ecosystem. The question is "as a developer working on my local machine OR using CI infrastructure, what vectors are there for someone to inject malicious code onto my systems?" Signing Key(?) AuthenticationThe
Presumably these are public signing keys.
Package (Derivation) Write AuthorityNext, I want to understand if I build and run a flake Transitive Pinning of SourceMy understanding of nix with flakes is that the author of Assuming github isn't compromised, That's about as good as modern systems get. (-unless we also committed Caching SecurityNext question: since we're relying on caching systems to substitute binaries for a given source, I'd like to document what attack surface that presents for malicious code injection. My guess is that the key material above are signing keys for caching systems, and I believe that's equivalent security to fetching files over https with a fingerprint-pinned certificate. In other words, there's a server with a private key that can serve up any arbitrary bytes, claiming they are the binaries corresponding to a given source package.
One saving grace for nix here is that every package build should be reproducible, so we could "spot" check caching servers by selecting a random transitive dependency and building it locally to verify it matches what the caching server claims.
|
Thanks, @shielded-nate, these are some great questions.
Correct. Accepting them allows you to use cached Nix artifacts rather than building everything locally. It’s also fairly easy to create your own cache to use, either on a service like Cachix, or self-hosted, so then you can eschew the ones you have less control over. The links below point to the source of the keys, and they match.
The author of A can also selectively pin transitive dependencies. E.g., inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/release-24.11";
b.url = "github:user/b";
b.inputs.nixpkgs.follows = "nixpkgs";
c.url = "github:org/c";
c.inputs.nixpkgs.follows = "nixpkgs";
d.url = "github:other-user/d";
d.inputs.nixpkgs.follows = "nixpkgs";
} ensures that NB: It should be possible to override inputs at arbitrarily depths, but there is a Nix bug that currently prevents it.
Yes. And the commands One other thing to be aware of here is that flakes still allow the traditional Nix ways of referencing other URLs, which are not directly reflected in flake.lock. On the plus side, a content hash is required, so any change to those non-flake dependencies results in a change to the flake itself. Here are two examples of what that can look like (assume this is referenced by user-project = stdenv.mkDerivation {
src = pkgs.fetchFromGitHub {
owner = "user";
repo = "project";
rev = "some-branch";
hash = "sha256-yMgDJ7D1pa37tHIX8SgO++eMqNCUOM0Bx+A5p10vWWg=";
};
patches = [
(pkgs.fetchpatch {
name = "fix-issue.patch";
url = "https://patch-diff.githubusercontent.com/raw/user/project/pull/103.patch";
hash = "sha256-/XhrSIKDqaitV3Kk+JkOgflgl3821m/8gLrP0yHENP0=";
})
];
}; These can still be overridden with {
inputs = {
b.url = "github:user/b";
user-project.url = "github:user/project/some-branch";
user-project.flake = false;
fix-user-project-issue.url = "https://patch-diff.githubusercontent.com/raw/user/project/pull/103.patch";
fix-user-project-issue.flake = false;
};
outputs = {b, user-project, fix-user-project-issue, ...}: {
…
overlays = final: prev: {
## Here is where the earlier derivation from `B` has its sources overridden.
user-project = b.user-project.overrideAttrs {
src = user-project;
patches = [fix-user-project-issue];
};
};
…
}
} But when you re-pin any upstream flake, you have to identify whether it’s pulling in other sources. There are various ways to explore the Nix derivation graph, though, so you don’t need to discover this by poring over the sources (especially if you only use parts of a flake, as is often the case, these dependencies may never even be fetched, let alone evaluated). There are probably good sources out there for how to ease this kind of auditing in Nix – commands for diffing the graphs, etc. But I haven’t explored that.
This is correct, at least as far as this PR and current common Nix practice goes. However, Nix has “beta-level” support for content-addressed derivations, which can eliminate the arbitrary-bytes issue. I am very interested in this, but haven’t used it at all, and can’t say how feasible it would be to use CA derivations for something like Zebra. Maybe it could at least reduce the number of derivations that aren’t content-addressed … but it’s probably a bigger initial win just to control the cache.
I don’t know if any tooling exists for this, but it should be easy enough to implement something basic. However, I feel like the number of transitive dependencies for any project is quite large, and only one needs to be compromised, which would then necessitate a lot of spot checks, so just managing our own cache is probably better. Also, while reproducibility is certainly a goal, the guarantees are limited. Content-addressed derivations will help enforce this more, and there are other limited uses of content hashes (like the sources for the derivation I showed above and fixed-output derivations). But it is very easy to create a derivation that isn’t reproducible: pkgs.runCommand "random" { } "echo $RANDOM > $out" I’m not sure where rustc lands on this, but GHC has a number of cases where its compiler output isn’t reproducible (and they’ve been addressing them as tools like Nix become more widespread). |
Thank you @sellout for this PR and @shielded-nate for reviewing it! We're not really sure what to do with this one since we don't have the expertise to review it and take care of any ongoing maintenance that could result from merging this PR. Is there anywhere else that this flake could be hosted? If so, we would be happy to link to it from our documentation. |
Yes! It’s easy to have the flake in its own repo, and to add this repo as an input. In that case I’m inclined to have it in I basically just need to add inputs.zebra.url = "github:ZcashFoundation/zebra/v2.1.0";
inputs.zebra.flake = false; to get it to fetch all the code from here rather than from the repo containing the flake. I initially put it here for the ease of my own Zebra development, but having it cloned in a separate directory and pointing it to my clone of Zebra by locally changing inputs.zebra.url = "github:ZcashFoundation/zebra/v2.1.0"; to inputs.zebra.url = "path:/local/path/to/zebra/clone"; is actually easier (so long as this PR remains unmerged), since I don’t have to merge/unmerge the branch as I make other changes to Zebra. |
Motivation
I manage all of my systems with Nix, so this grew out of me doing various work around Zebra the past few months. I currently merge this branch into whatever I’m working on (which is easy, because this basically only adds new files1, so no conflicts).
I also saw @teor2345 had previously put up a Nix derivation in #1479, which roughly corresponds to nix/packages/zebra in this PR.
Solution
Here is a summary of what’s added.
${system}
can be replaced with any ofaarch64-darwin
,aarch64-linux
,x86_64-darwin
,x86_64-linux
.packages
are builds of the main content of this repochecks
are various tests (other thancargo test
, which is covered bypackages
)*Modules
allow you to configure zebrad like this*Configurations
are examples of those configurations that are built as tests of much of the flakedevShells
provide a sandboxed development environment withrustc
,cargo
, etc.Tests
Everything is built on aarch64-darwin, aarch64-linux, and x86_64-linux at garnix, which also runs the various checks (clippy, fmt, etc.), and it builds the example configurations which implicitly tests the overlays, modules, etc.
For these CI builds to run on not-my-fork, the ZcashFoundation org would need to get a garnix account, or would need to add some Nix-based GitHub workflow (ideally with some caching solution, which garnix handles automatically).
Follow-up Work
The current solution has everything in there, but I think the Nix/Rust tooling could be improved wrt cache-friendliness. It currently uses crane, which condenses everything into only two packages (“zebra”, containing all the crates in this repo, and “zebra-deps”, containing all the dependency crates not in this repo), so if any part of Zebra changes, all of Zebra gets rebuilt, and if a dependency changes, all of Zebra and all deps get rebuilt. Having a separate package for each crate would minimize rebuilds, so a solution like cargo2nix or crate2nix is probably the way to go longer-term2.
The derivation is built with
ZEBRA_SKIP_NETWORK_TESTS
, because of Nix sandboxing3. But even so, there are a number of failing tests that I’ve explicitly skipped. That file conditionalizes them so you can see in which contexts they fail. One thing I didn’t conditionally enable is tests that pass outside of a sandbox, because I think that makes the dev / CI divide confusing. E.g., a number of disabled tests can pass if__darwinAllowLocalNetworking
is enabled, but that can only be done outside of a sandbox. Also interesting is that there are a number of tests that only fail when theelasticsearch
feature is enabled (and only on MacOS).This PR doesn’t provide a default.nix or shell.nix (#1479 did, just under a different name), because I do everything with flakes, but it’s easy enough to expose them with flake-compat if that’s desired.
PR Author's Checklist
PR Reviewer's Checklist
Footnotes
There are three minor changes to existing files:
Alias
from systemd/zebrad.service.They’re not being used yet because of a bug in Nix ([OS X] Derivation fails with sandbox NixOS/nix#4119) that prevents packages with a lot of dependencies from building in a sandbox on MacOS (which would cause failures on garnix CI, as it requires everything to be sandboxed). ↩
There are ways to enable network access in a sandbox, so that might allow all (or at least more) tests to be enabled. ↩