All Projects → ocharles → nix-build-cache

ocharles / nix-build-cache

Licence: MIT license
Extends stdenv.mkDerivation based builds with a distributed build cache on Amazon S3.

Programming Languages

Nix
1067 projects

Labels

Projects that are alternatives of or similar to nix-build-cache

dotfiles
My NixOS configuration featuring awesome and neovim
Stars: ✭ 40 (+66.67%)
Mutual labels:  nixos
nixconfig
My NixOS config
Stars: ✭ 67 (+179.17%)
Mutual labels:  nixos
nixos-configuration
A repo for my nixos configuration files
Stars: ✭ 20 (-16.67%)
Mutual labels:  nixos
fromElisp
An Emacs Lisp reader in Nix.
Stars: ✭ 26 (+8.33%)
Mutual labels:  nixos
comma
Comma runs software without installing it. [maintainers=@Artturin,@burke,@DavHau]
Stars: ✭ 626 (+2508.33%)
Mutual labels:  nixos
dns.nix
A Nix DSL for DNS zone files
Stars: ✭ 69 (+187.5%)
Mutual labels:  nixos
Mach Nix
Create highly reproducible python environments
Stars: ✭ 231 (+862.5%)
Mutual labels:  nixos
crane
A Nix library for building cargo projects. Never build twice thanks to incremental artifact caching.
Stars: ✭ 348 (+1350%)
Mutual labels:  nixos
nixos-installer
Combining the power of Nix, Guile & Elm to install NixOS
Stars: ✭ 14 (-41.67%)
Mutual labels:  nixos
nix-config
A collection of my system configs and dotfiles
Stars: ✭ 35 (+45.83%)
Mutual labels:  nixos
gradle2nix
Generate Nix expressions which build Gradle-based projects.
Stars: ✭ 71 (+195.83%)
Mutual labels:  nixos
dotfiles
I showed you my source code, pls respond
Stars: ✭ 45 (+87.5%)
Mutual labels:  nixos
nix-configs
My Nix{OS} configuration files
Stars: ✭ 54 (+125%)
Mutual labels:  nixos
rc
Structured system configuration (I moved from NixOS to GuixSD)
Stars: ✭ 97 (+304.17%)
Mutual labels:  nixos
dotfiles
For keeping all my Dotfiles update to date
Stars: ✭ 29 (+20.83%)
Mutual labels:  nixos
Nox
Tools to make nix nicer to use
Stars: ✭ 232 (+866.67%)
Mutual labels:  nixos
nixos-raspberry-pi-cluster
A user-guide to create a Raspberry Pi (3B+, 4) cluster under NixOS and managed by NixOps
Stars: ✭ 69 (+187.5%)
Mutual labels:  nixos
nixvim
Configure Neovim with Nix!
Stars: ✭ 120 (+400%)
Mutual labels:  nixos
impermanence
Modules to help you handle persistent state on systems with ephemeral root storage [maintainer=@talyz]
Stars: ✭ 401 (+1570.83%)
Mutual labels:  nixos
nix-xdg
[WIP] Nix overlay for making programs xdg compliant
Stars: ✭ 18 (-25%)
Mutual labels:  nixos

nix-build-cache

nix-build-cache extends stdenv.mkDerivation based builds with a distributed build cache. Many build tools produce intermediate files as part of a build, and if these intermediate files are present for subsequent build, steps of the build can be eliminated. nix-build-cache manages these files in a somewhat impure manner, to avoid the output hash changing. Essentially, these build cache files are purely optimisations and as such shouldn't impact the resulting output.

Usage

nix-build-cache is a function of the following type:

{ aws-key : Text
, aws-secret : Text
, pkgs : Nixpkgs
, cache-name : Text
, master-cache : Text
, cache-dirs : List Text
, s3-bucket : Text
} -> stdenv.mkDerivation-result -> stdenv.mkDeriavtion-result

where Nixpkgs is the object that you'd get from import <nixpkgs> {}, and stdenv.mkDerivation-result is the result of a well-formed call to stdenv.mkDerivation. To use nix-build-cache, supply the configuration object and a build that you wish to add a cache to. For example, here's how we can add a build cache to a build of Aeson:

let pkgs = import <nixpkgs> {};
    add-build-cache = 
      import 
        ./nix-build-cache.nix 
        {  
          inherit pkgs;
          aws-key = "xxx";
          aws-secret = "xxx";
          cache-name = "aeson";
          master-cache = "aeson";
          cache-dirs = [ "dist" ];
          s3-bucket = "aeson-build";
        };

in add-build-cache pkgs.haskellPackages.aeson

The parameters supplied to nix-build-cache are:

  • aws-key: An AWS key
  • aws-secret-key: An AWS secret key
  • pkgs: A nixpkgs repository. Used to call things like mktemp and aws.
  • cache-name: The name of the cache for this build. A combination of a Git repository name and a Git branch name is a good choice here.
  • master-cache: The name of a fallback cache, if the cache under cache-name can't be found. If you're building GitHub pull requests, this means the first build can resume from the master cache.
  • cache-dirs: Which directories to add to the cache after the build completes. For Haskell projects, you will want to use [ "dist" ], and for Elm projects [ "elm-stuff" ].
  • s3-bucket: The name of the S3 bucket to upload the cache to.

How nix-build-cache Works

nix-build-cache augments a build by adding some extra steps to preConfigure and preInstall.

In preConfigure, we first generate an MD5 hash of all files in the source tree. Next, we attempt to download either cache-name or master-cache from S3, and unpack it. This build cache is expected to contain a file - MD5SUMS - which contains the MD5 hash of all source files that produced the cache. We join the current MD5 hashes against these hashes, and work out which have changed. Any changed files are then touched to update their timestamp. Finally, all artifacts in the build cache are touched to be two hours in the past. This is an arbitrary time difference, but it's important the build cache files look they were produced before the changed files were changed.

In preInstall, we simply tar up the directories mentioned in cache-dirs and upload this tarball to cache-name.

Limitations

This approach relies on builds having network access.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].