zhaofengli / attic

Multi-tenant Nix Binary Cache
https://docs.attic.rs
Other
965 stars 73 forks source link

Nix-Darwin/MacOS hosted example #114

Closed heywoodlh closed 7 months ago

heywoodlh commented 7 months ago

First, thank you for a great project!

I wrote a guide on how I'm using attic on a MacOS machine using nix-darwin and it seems to be working well for me: https://discourse.nixos.org/t/nix-binary-cache-for-macos-nix-darwin-with-attic/40118

Wanted to post here as I couldn't find examples of how to use attic on a MacOS host. Here's my config at the time of writing for reference:

{ config, pkgs, lib, attic, ... }:

let
  system = pkgs.system;
  atticServer = attic.packages.${system}.attic-server;
  atticClient = attic.packages.${system}.attic-client;
  garbageCollectCache = pkgs.writeShellScriptBin "nix-darwin-cache-garbage-collect" ''
    ${atticServer}/bin/atticd --mode garbage-collector-once &>>/tmp/binary-cache.log
  '';
  populateCache = pkgs.writeShellScriptBin "nix-darwin-cache-populate" ''
    rm -rf /tmp/nixos-configs
    ${pkgs.git}/bin/git clone https://github.com/heywoodlh/nixos-configs /tmp/nixos-configs
    cd /tmp/nixos-configs
    # aarch64 build
    ${pkgs.nix}/bin/nix build .#darwinConfigurations.mac-mini.config.system.build.toplevel
    ${atticClient}/bin/attic push nix-darwin ./result

    rm -rf /tmp/nixos-configs
  '';
  runCache = pkgs.writeShellScript "serve-cache" ''
    ${atticServer}/bin/atticd --listen 0.0.0.0:8080 &>>/tmp/binary-cache.log
  '';
in {
  launchd.daemons.cache-populate = {
    command = "${populateCache}/bin/nix-darwin-cache-populate";
    serviceConfig.StartInterval = 86400; # run once a day
  };

  launchd.daemons.cache-garbage-collect = {
    command = "${populateCache}/bin/nix-darwin-cache-garbage-collect";
    serviceConfig.StartInterval = 604800; # run once a week
  };

  launchd.daemons.nix-cache = {
    command = "${runCache}";
    serviceConfig.RunAtLoad = true;
  };

  environment.systemPackages = [
    garbageCollectCache
    populateCache
  ];
}

I should probably write a Nix-Darwin module for this, but haven't yet. :smile:

Will be closing this, but wanted to post here for anyone with a similar use-case (I hope this isn't an unwelcome issue!).