dustinlyons / nixos-config

General purpose Nix configuration for macOS / NixOS with starter templates + step-by-step guides ✨
BSD 3-Clause "New" or "Revised" License
1.79k stars 113 forks source link

Using binary cache on inital build #111

Closed danielhep closed 1 month ago

danielhep commented 2 months ago

Following your instructions on MacOS, I'm running the first nix run .#build and I've encountered some very expensive and long running compilation steps.

❯ nix run .#build
warning: Git tree '/Users/danielhep/git/nixos-config' is dirty
Running build for aarch64-darwin
Starting build...
warning: Git tree '/Users/danielhep/git/nixos-config' is dirty
[1/33/87 built, 0.0 MiB DL] building swift-5.8 (buildPhase): [2534/3532] Building CXX object tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/CGExprScalar.cpp.o

Shouldn't Nix be using the binary cache instead of recompiling swift from source? It was compiling emacs too but I commented that package out for now to save some time.

I'm not entirely sure how cachix works here but I expected it to provide the binary. Maybe it needs to complete the first setup to begin using cachix? It would be nice to have a binary cache bootstrapped on the first setup to save time so I don't have to wait hours for everything to compile.

danielhep commented 2 months ago

I had home manager installed before, which I thought maybe was causing issues, so I uninstalled it and also uninstalled nix entirely and reinstalled it. Starting fresh, it still is trying to build swift from scratch (I let this run for an hour and it wasn't done so I just cancelled it). Then I tried manually installing cachix and ran cachix use with the caches listed in cachix/default.nix in the repo, but no luck. I am taking stabs in the dark because I have a poor understanding of how the binary cache works, and I couldn't find much documentation on it.

dustinlyons commented 2 months ago

@danielhep This was due to an issue with the swift build. As the template defaults nixpkgs to nixos-unstable, you'll encounter cache misses from time to time if either

a) the build for a package is failing b) hydra (nixpkgs cache builder) hasn't caught up to the latest packages locked in the nixpkgs flake

Looks like a PR to fix swift was merged 20 hours ago, so I imagine over the next few days, as hydra catches up, the issue will go away.

https://github.com/NixOS/nixpkgs/issues/327836

dustinlyons commented 2 months ago

@danielhep After first exploration on this, I think this is a bug. In darwin/hosts/default.nix, we use Nix imports to bring in cachix, but then we override that when setting the nix block in the file. So we need to likely just drop the shared cachix and handle it natively inside each host.

I'm testing this now but can update the templates soon.

My darwin/hosts/default.nix now looks like:

{ agenix, config, pkgs, ... }:

let user = "dustin"; in
{

  imports = [
    ../../modules/darwin/secrets.nix
    ../../modules/darwin/home-manager.nix
    ../../modules/shared
     agenix.darwinModules.default
  ];

  # Auto upgrade nix package and the daemon service.
  services.nix-daemon.enable = true;

  # Setup user, packages, programs
  nix = {
    package = pkgs.nix;

    settings.trusted-users = [ "@admin" "${user}" ];
    settings.substituters = [ "https://cache.nixos.org" ];
    settings.trusted-public-keys = [ "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=" ];

    gc = {
      user = "root";
      automatic = true;
      interval = { Weekday = 0; Hour = 2; Minute = 0; };
      options = "--delete-older-than 30d";
    };

    # Turn this on to make command line easier
    extraOptions = ''
      experimental-features = nix-command flakes
    '';
  };

  # Turn off NIX_PATH warnings now that we're using flakes
  system.checks.verifyNixPath = false;

  # Load configuration that is shared across systems
  environment.systemPackages = with pkgs; [
    emacs-unstable
    agenix.packages."${pkgs.system}".default
  ] ++ (import ../../modules/shared/packages.nix { inherit pkgs; });

  launchd.user.agents.emacs.path = [ config.environment.systemPath ];
  launchd.user.agents.emacs.serviceConfig = {
    KeepAlive = true;
    ProgramArguments = [
      "/bin/sh"
      "-c"
      "{ osascript -e 'display notification \"Attempting to start Emacs...\" with title \"Emacs Launch\"'; /bin/wait4path ${pkgs.emacs}/bin/emacs && { ${pkgs.emacs}/bin/emacs --fg-daemon; if [ $? -eq 0 ]; then osascript -e 'display notification \"Emacs has started.\" with title \"Emacs Launch\"'; else osascript -e 'display notification \"Failed to start Emacs.\" with title \"Emacs Launch\"' >&2; fi; } } &> /tmp/emacs_launch.log"
    ];
    StandardErrorPath = "/tmp/emacs.err.log";
    StandardOutPath = "/tmp/emacs.out.log";
  };

  system = {
    stateVersion = 4;

    defaults = {
      LaunchServices = {
        LSQuarantine = false;
      };

      NSGlobalDomain = {
        AppleShowAllExtensions = true;
        ApplePressAndHoldEnabled = false;

        # 120, 90, 60, 30, 12, 6, 2
        KeyRepeat = 2;

        # 120, 94, 68, 35, 25, 15
        InitialKeyRepeat = 15;

        "com.apple.mouse.tapBehavior" = 1;
        "com.apple.sound.beep.volume" = 0.0;
        "com.apple.sound.beep.feedback" = 0;
      };

      dock = {
        autohide = false;
        show-recents = false;
        launchanim = true;
        mouse-over-hilite-stack = true;
        orientation = "bottom";
        tilesize = 48;
      };

      finder = {
        _FXShowPosixPathInTitle = false;
      };

      trackpad = {
        Clicking = true;
        TrackpadThreeFingerDrag = true;
      };
    };

    keyboard = {
      enableKeyMapping = true;
      remapCapsLockToControl = true;
    };
  };
}
dustinlyons commented 1 month ago

This has been resolved in the template. Let me know if any further questions and I can reopen.