|
| 1 | +# Nix Caching Setup |
| 2 | + |
| 3 | +This repository uses multiple caching strategies for optimal CI/CD performance. |
| 4 | + |
| 5 | +## Caching Layers |
| 6 | + |
| 7 | +### 1. Cachix Binary Cache |
| 8 | +**Public cache**: `singularity-ng` |
| 9 | + |
| 10 | +Stores: |
| 11 | +- Built Nix derivations |
| 12 | +- Development shells |
| 13 | +- All package outputs |
| 14 | + |
| 15 | +**Setup for users:** |
| 16 | +```bash |
| 17 | +# Install cachix |
| 18 | +nix-env -iA cachix -f https://cachix.org/api/v1/install |
| 19 | + |
| 20 | +# Use the cache |
| 21 | +cachix use singularity-ng |
| 22 | +``` |
| 23 | + |
| 24 | +Or add to `~/.config/nix/nix.conf`: |
| 25 | +``` |
| 26 | +substituters = https://cache.nixos.org https://singularity-ng.cachix.org |
| 27 | +trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= singularity-ng.cachix.org-1:your-signing-key-here |
| 28 | +``` |
| 29 | + |
| 30 | +### 2. Magic Nix Cache |
| 31 | +**GitHub Actions**: Automatic caching via `magic-nix-cache-action` |
| 32 | + |
| 33 | +Benefits: |
| 34 | +- Zero configuration |
| 35 | +- Automatic cache invalidation |
| 36 | +- Works across workflow runs |
| 37 | +- Free for public repos |
| 38 | + |
| 39 | +### 3. FlakeHub |
| 40 | +**Flake registry**: Published flake for easy consumption |
| 41 | + |
| 42 | +Use in your `flake.nix`: |
| 43 | +```nix |
| 44 | +{ |
| 45 | + inputs = { |
| 46 | + singularity-workflows.url = "https://flakehub.com/f/Singularity-ng/singularity-workflows/*.tar.gz"; |
| 47 | + }; |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +## CI Workflows |
| 52 | + |
| 53 | +### nix-ci.yml |
| 54 | +Full Nix-based CI: |
| 55 | +- Uses DeterminateSystems/nix-installer-action |
| 56 | +- Integrates Cachix + Magic Nix Cache |
| 57 | +- Runs tests in nix develop shell |
| 58 | +- ~2-3 minutes (cached) |
| 59 | + |
| 60 | +### cachix-push.yml |
| 61 | +Weekly cache refresh: |
| 62 | +- Rebuilds all derivations |
| 63 | +- Pushes to Cachix |
| 64 | +- Runs on schedule + manual trigger |
| 65 | +- Keeps cache warm |
| 66 | + |
| 67 | +### flakehub-publish.yml |
| 68 | +FlakeHub publishing: |
| 69 | +- Triggered on version tags |
| 70 | +- Makes flake available via FlakeHub |
| 71 | +- Easy consumption for users |
| 72 | + |
| 73 | +## Cache Performance |
| 74 | + |
| 75 | +**First build (cold cache):** |
| 76 | +- ~5-8 minutes (downloads everything) |
| 77 | + |
| 78 | +**Subsequent builds (warm cache):** |
| 79 | +- ~1-2 minutes (everything cached) |
| 80 | + |
| 81 | +**With Cachix:** |
| 82 | +- Dev shell: ~30s (pull from cache) |
| 83 | +- Full build: ~1-2 minutes |
| 84 | + |
| 85 | +## Secrets Required |
| 86 | + |
| 87 | +Setup in GitHub repository secrets: |
| 88 | + |
| 89 | +1. `CACHIX_AUTH_TOKEN` |
| 90 | + - Get from: https://app.cachix.org |
| 91 | + - Needed for: cachix-push.yml |
| 92 | + |
| 93 | +2. FlakeHub (optional) |
| 94 | + - Automatic via GitHub App |
| 95 | + - Or set `FLAKEHUB_TOKEN` |
| 96 | + |
| 97 | +## Local Development |
| 98 | + |
| 99 | +The flake includes Cachix configuration: |
| 100 | + |
| 101 | +```nix |
| 102 | +nixConfig = { |
| 103 | + extra-substituters = [ "https://singularity-ng.cachix.org" ]; |
| 104 | + extra-trusted-public-keys = [ "singularity-ng.cachix.org-1:key" ]; |
| 105 | +}; |
| 106 | +``` |
| 107 | + |
| 108 | +This is automatically applied when you run: |
| 109 | +```bash |
| 110 | +nix develop |
| 111 | +``` |
| 112 | + |
| 113 | +## Monitoring |
| 114 | + |
| 115 | +- Cachix dashboard: https://app.cachix.org/cache/singularity-ng |
| 116 | +- FlakeHub page: https://flakehub.com/flake/Singularity-ng/singularity-workflows |
| 117 | +- GitHub Actions: Check workflow runs for cache hit rates |
| 118 | + |
| 119 | +## Best Practices |
| 120 | + |
| 121 | +1. **Weekly cache updates**: Scheduled via cachix-push.yml |
| 122 | +2. **Lock file updates**: Keep flake.lock current for security |
| 123 | +3. **Cache warming**: Run cachix-push after major dependency updates |
| 124 | +4. **Monitor size**: Large caches cost more on Cachix (free tier: 10GB) |
| 125 | + |
| 126 | +## Troubleshooting |
| 127 | + |
| 128 | +### Cache misses |
| 129 | +```bash |
| 130 | +# Clear local Nix store cache |
| 131 | +nix-collect-garbage -d |
| 132 | + |
| 133 | +# Rebuild with fresh cache |
| 134 | +nix develop --refresh |
| 135 | +``` |
| 136 | + |
| 137 | +### Cachix authentication |
| 138 | +```bash |
| 139 | +# Re-authenticate |
| 140 | +cachix authtoken <YOUR_TOKEN> |
| 141 | + |
| 142 | +# Test push |
| 143 | +nix build .#devShells.x86_64-linux.default |
| 144 | +cachix push singularity-ng result |
| 145 | +``` |
| 146 | + |
| 147 | +### FlakeHub updates not visible |
| 148 | +- Check workflow runs in Actions tab |
| 149 | +- Verify tag format: `v*` (e.g., v0.1.6) |
| 150 | +- Check FlakeHub dashboard for errors |
| 151 | + |
| 152 | +## Cost Considerations |
| 153 | + |
| 154 | +- **Cachix free tier**: 10GB storage, unlimited downloads |
| 155 | +- **Magic Nix Cache**: Free (GitHub Actions feature) |
| 156 | +- **FlakeHub**: Free for public flakes |
| 157 | + |
| 158 | +If cache exceeds 10GB: |
| 159 | +- Consider paid Cachix plan |
| 160 | +- Or switch to self-hosted cache (nixserve, S3) |
0 commit comments