Skip to content

Commit 10794da

Browse files
authored
Merge pull request #78 from nix-community/unit-suffixes
Support human-readable inputs for store size
2 parents 40df369 + 086d05e commit 10794da

File tree

13 files changed

+255
-57
lines changed

13 files changed

+255
-57
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ jobs:
350350
# except the version with the `primary-key`, if it exists
351351
purge-primary-key: never
352352
# and collect garbage in the Nix store until it reaches this size in bytes
353-
gc-max-store-size: 0
353+
gc-max-store-size: 0M
354354

355355
# Uncomment to debug this job
356356
# - name: Setup tmate session

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ See [Caching Approaches](#caching-approaches).
8484
restore-prefixes-first-match: nix-${{ runner.os }}-
8585
# collect garbage until Nix store size (in bytes) is at most this number
8686
# before trying to save a new cache
87-
# 1 GB = 1073741824 B
88-
gc-max-store-size-linux: 1073741824
87+
# 1G = 1073741824
88+
gc-max-store-size-linux: 1G
8989
# do purge caches
9090
purge: true
9191
# purge all versions of the cache
@@ -134,7 +134,7 @@ See [action.yml](action.yml).
134134
| `paths-macos` | <ul> <li>Overrides <code>paths</code>.</li> <li>Can have an effect only when the action runs on a <code>macOS</code> runner.</li> </ul> | `false` | `""` |
135135
| `paths-linux` | <ul> <li>Overrides <code>paths</code>.</li> <li>Can have an effect only when the action runs on a <code>Linux</code> runner.</li> </ul> | `false` | `""` |
136136
| `backend` | <p>Choose an implementation of the <code>cache</code> package.</p> <ul> <li>When <code>actions</code>, use the <a href="https://github.com/actions/toolkit/tree/main/packages/cache">actions version</a> from <a href="https://github.com/nix-community/cache-nix-action/tree/actions-toolkit/packages/cache">here</a>.</li> <li>When <code>buildjet</code>, use the <a href="https://github.com/BuildJet/toolkit/tree/main/packages/cache-buildjet">BuildJet version</a> from <a href="https://github.com/nix-community/cache-nix-action/tree/buildjet-toolkit/packages/cache">here</a>.</li> </ul> | `false` | `actions` |
137-
| `gc-max-store-size` | <ul> <li>Can have an effect only when <code>nix: true</code>, <code>save: true</code>.</li> <li>The input has no effect if "primary-key" hit occurs when starting to save the new cache.</li> <li>When a number, the action collects garbage (via <code>nix store gc --max ...</code>) until the Nix store size (in bytes) is at most this number just before the action tries to save a new cache.</li> <li>Otherwise, this input has no effect.</li> </ul> | `false` | `""` |
137+
| `gc-max-store-size` | <ul> <li>Can have an effect only when <code>nix: true</code>, <code>save: true</code>.</li> <li>The input has no effect if "primary-key" hit occurs when starting to save the new cache.</li> <li>When a non-negative integer number (possibly with a suffix), the action collects garbage (via <code>nix store gc --max ...</code>) until the Nix store size (in bytes) is at most this number just before the action tries to save a new cache.</li> <li>If you specify a suffix, it must be <code>K</code> (kibibytes, 2 ^ 10 bytes), <code>M</code> (mebibytes, 2 ^ 20 bytes) or <code>G</code> (gibibytes, 2 ^ 30 bytes), where <code>2 ^ N</code> means <code>2 to the power N</code>.</li> <li>Otherwise, this input has no effect.</li> </ul> | `false` | `""` |
138138
| `gc-max-store-size-macos` | <ul> <li>Overrides <code>gc-max-store-size</code>.</li> <li>Can have an effect only when the action runs on a <code>macOS</code> runner.</li> </ul> | `false` | `""` |
139139
| `gc-max-store-size-linux` | <ul> <li>Overrides <code>gc-max-store-size</code>.</li> <li>Can have an effect only when the action runs on a <code>Linux</code> runner.</li> </ul> | `false` | `""` |
140140
| `purge` | <ul> <li>When <code>true</code>, the action purges (possibly zero) caches.</li> <li>The action purges only caches scoped to the current <a href="https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables">GITHUB_REF</a>.</li> <li>Otherwise, this input has no effect.</li> </ul> | `false` | `false` |

action.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ in
163163
]
164164
}
165165
- The input has no effect if "${primary-key}" hit occurs when starting to save the new cache.
166-
- When a non-negative number, the action collects garbage (via `nix store gc --max ...`) until the Nix store size (in bytes) is at most this number just before the action tries to save a new cache.
166+
- When a non-negative integer number (possibly with a suffix), the action collects garbage (via `nix store gc --max ...`) until the Nix store size (in bytes) is at most this number just before the action tries to save a new cache.
167+
- If you specify a suffix, it must be `K` (kibibytes, 2 ^ 10 bytes), `M` (mebibytes, 2 ^ 20 bytes) or `G` (gibibytes, 2 ^ 30 bytes), where `2 ^ N` means `2 to the power N`.
167168
- ${noEffectOtherwise}
168169
default: ""
169170
${gc-max-store-size}-macos:

action.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ inputs:
8888
description: |
8989
- Can have an effect only when `nix: true`, `save: true`.
9090
- The input has no effect if "primary-key" hit occurs when starting to save the new cache.
91-
- When a number, the action collects garbage (via `nix store gc --max ...`) until the Nix store size (in bytes) is at most this number just before the action tries to save a new cache.
91+
- When a non-negative integer number (possibly with a suffix), the action collects garbage (via `nix store gc --max ...`) until the Nix store size (in bytes) is at most this number just before the action tries to save a new cache.
92+
- If you specify a suffix, it must be `K` (kibibytes, 2 ^ 10 bytes), `M` (mebibytes, 2 ^ 20 bytes) or `G` (gibibytes, 2 ^ 30 bytes), where `2 ^ N` means `2 to the power N`.
9293
- Otherwise, this input has no effect.
9394
default: ""
9495
gc-max-store-size-macos:

dist/restore-only/index.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83753,7 +83753,8 @@ var __importStar = (this && this.__importStar) || (function () {
8375383753
};
8375483754
})();
8375583755
Object.defineProperty(exports, "__esModule", ({ value: true }));
83756-
exports.backend = exports.Backend = exports.token = exports.uploadChunkSize = exports.purgeCreated = exports.purgeLastAccessed = exports.purgePrefixes = exports.purgePrimaryKey = exports.purge = exports.gcMaxStoreSize = exports.save = exports.paths = exports.nix = exports.isMacos = exports.isLinux = exports.failOn = exports.skipRestoreOnHitPrimaryKey = exports.restorePrefixesAllMatches = exports.restorePrefixesFirstMatch = exports.primaryKey = void 0;
83756+
exports.backend = exports.Backend = exports.token = exports.uploadChunkSize = exports.purgeCreated = exports.purgeLastAccessed = exports.purgePrefixes = exports.purgePrimaryKey = exports.purge = exports.gcMaxStoreSize = exports.gcMaxStoreSizeInputName = exports.save = exports.paths = exports.nix = exports.isMacos = exports.isLinux = exports.failOn = exports.skipRestoreOnHitPrimaryKey = exports.restorePrefixesAllMatches = exports.restorePrefixesFirstMatch = exports.primaryKey = void 0;
83757+
exports.choose = choose;
8375783758
const core = __importStar(__nccwpck_require__(7484));
8375883759
const constants_1 = __nccwpck_require__(7242);
8375983760
const utils = __importStar(__nccwpck_require__(9612));
@@ -83809,10 +83810,11 @@ exports.paths = (exports.nix
8380983810
return paths;
8381083811
})());
8381183812
exports.save = utils.getInputAsBool(constants_1.Inputs.Save);
83813+
exports.gcMaxStoreSizeInputName = choose(constants_1.Inputs.GCMaxStoreSizeLinux, constants_1.Inputs.GCMaxStoreSizeMacos, constants_1.Inputs.GCMaxStoreSize);
8381283814
exports.gcMaxStoreSize = exports.nix
8381383815
? (function () {
83814-
const gcMaxStoreSize = utils.getInputAsInt(constants_1.Inputs.GCMaxStoreSize);
83815-
const gcMaxStoreSizePlatform = utils.getInputAsInt(choose(constants_1.Inputs.GCMaxStoreSizeLinux, constants_1.Inputs.GCMaxStoreSizeMacos, constants_1.Inputs.GCMaxStoreSize));
83816+
const gcMaxStoreSize = utils.parseNixGcMax(constants_1.Inputs.GCMaxStoreSize);
83817+
const gcMaxStoreSizePlatform = utils.parseNixGcMax(exports.gcMaxStoreSizeInputName);
8381683818
return gcMaxStoreSizePlatform !== undefined
8381783819
? gcMaxStoreSizePlatform
8381883820
: gcMaxStoreSize;
@@ -84725,6 +84727,7 @@ var __importStar = (this && this.__importStar) || (function () {
8472584727
})();
8472684728
Object.defineProperty(exports, "__esModule", ({ value: true }));
8472784729
exports.getInputAsArray = getInputAsArray;
84730+
exports.parseNixGcMax = parseNixGcMax;
8472884731
exports.getInputAsInt = getInputAsInt;
8472984732
exports.getInputAsBool = getInputAsBool;
8473084733
const core = __importStar(__nccwpck_require__(7484));
@@ -84735,6 +84738,41 @@ function getInputAsArray(name, options) {
8473584738
.map(s => s.replace(/^!\s+/, "!").trim())
8473684739
.filter(x => x !== "");
8473784740
}
84741+
// https://github.com/NixOS/nix/blob/a047dec120672d00e069bacf10ffdda420fd1048/src/libutil/util.hh#L88
84742+
function parseNixGcMax(name, options) {
84743+
const input = core.getInput(name, options);
84744+
if (input.length == 0) {
84745+
return undefined;
84746+
}
84747+
const chars = [...input];
84748+
let result = 0;
84749+
for (let i = 0; i < chars.length; i++) {
84750+
const char = chars[i];
84751+
const digit = parseInt(char);
84752+
if (!isNaN(digit)) {
84753+
result = result * 10 + digit;
84754+
}
84755+
else {
84756+
if (i == chars.length - 1) {
84757+
switch (char) {
84758+
case "K":
84759+
result <<= 10;
84760+
case "M":
84761+
result <<= 20;
84762+
case "G":
84763+
result <<= 30;
84764+
default:
84765+
result = NaN;
84766+
}
84767+
}
84768+
else {
84769+
result = NaN;
84770+
break;
84771+
}
84772+
}
84773+
}
84774+
return isNaN(result) ? undefined : { input, value: result };
84775+
}
8473884776
function getInputAsInt(name, options) {
8473984777
const value = parseInt(core.getInput(name, options));
8474084778
if (isNaN(value) || value < 0) {

dist/restore/index.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83753,7 +83753,8 @@ var __importStar = (this && this.__importStar) || (function () {
8375383753
};
8375483754
})();
8375583755
Object.defineProperty(exports, "__esModule", ({ value: true }));
83756-
exports.backend = exports.Backend = exports.token = exports.uploadChunkSize = exports.purgeCreated = exports.purgeLastAccessed = exports.purgePrefixes = exports.purgePrimaryKey = exports.purge = exports.gcMaxStoreSize = exports.save = exports.paths = exports.nix = exports.isMacos = exports.isLinux = exports.failOn = exports.skipRestoreOnHitPrimaryKey = exports.restorePrefixesAllMatches = exports.restorePrefixesFirstMatch = exports.primaryKey = void 0;
83756+
exports.backend = exports.Backend = exports.token = exports.uploadChunkSize = exports.purgeCreated = exports.purgeLastAccessed = exports.purgePrefixes = exports.purgePrimaryKey = exports.purge = exports.gcMaxStoreSize = exports.gcMaxStoreSizeInputName = exports.save = exports.paths = exports.nix = exports.isMacos = exports.isLinux = exports.failOn = exports.skipRestoreOnHitPrimaryKey = exports.restorePrefixesAllMatches = exports.restorePrefixesFirstMatch = exports.primaryKey = void 0;
83757+
exports.choose = choose;
8375783758
const core = __importStar(__nccwpck_require__(7484));
8375883759
const constants_1 = __nccwpck_require__(7242);
8375983760
const utils = __importStar(__nccwpck_require__(9612));
@@ -83809,10 +83810,11 @@ exports.paths = (exports.nix
8380983810
return paths;
8381083811
})());
8381183812
exports.save = utils.getInputAsBool(constants_1.Inputs.Save);
83813+
exports.gcMaxStoreSizeInputName = choose(constants_1.Inputs.GCMaxStoreSizeLinux, constants_1.Inputs.GCMaxStoreSizeMacos, constants_1.Inputs.GCMaxStoreSize);
8381283814
exports.gcMaxStoreSize = exports.nix
8381383815
? (function () {
83814-
const gcMaxStoreSize = utils.getInputAsInt(constants_1.Inputs.GCMaxStoreSize);
83815-
const gcMaxStoreSizePlatform = utils.getInputAsInt(choose(constants_1.Inputs.GCMaxStoreSizeLinux, constants_1.Inputs.GCMaxStoreSizeMacos, constants_1.Inputs.GCMaxStoreSize));
83816+
const gcMaxStoreSize = utils.parseNixGcMax(constants_1.Inputs.GCMaxStoreSize);
83817+
const gcMaxStoreSizePlatform = utils.parseNixGcMax(exports.gcMaxStoreSizeInputName);
8381683818
return gcMaxStoreSizePlatform !== undefined
8381783819
? gcMaxStoreSizePlatform
8381883820
: gcMaxStoreSize;
@@ -84725,6 +84727,7 @@ var __importStar = (this && this.__importStar) || (function () {
8472584727
})();
8472684728
Object.defineProperty(exports, "__esModule", ({ value: true }));
8472784729
exports.getInputAsArray = getInputAsArray;
84730+
exports.parseNixGcMax = parseNixGcMax;
8472884731
exports.getInputAsInt = getInputAsInt;
8472984732
exports.getInputAsBool = getInputAsBool;
8473084733
const core = __importStar(__nccwpck_require__(7484));
@@ -84735,6 +84738,41 @@ function getInputAsArray(name, options) {
8473584738
.map(s => s.replace(/^!\s+/, "!").trim())
8473684739
.filter(x => x !== "");
8473784740
}
84741+
// https://github.com/NixOS/nix/blob/a047dec120672d00e069bacf10ffdda420fd1048/src/libutil/util.hh#L88
84742+
function parseNixGcMax(name, options) {
84743+
const input = core.getInput(name, options);
84744+
if (input.length == 0) {
84745+
return undefined;
84746+
}
84747+
const chars = [...input];
84748+
let result = 0;
84749+
for (let i = 0; i < chars.length; i++) {
84750+
const char = chars[i];
84751+
const digit = parseInt(char);
84752+
if (!isNaN(digit)) {
84753+
result = result * 10 + digit;
84754+
}
84755+
else {
84756+
if (i == chars.length - 1) {
84757+
switch (char) {
84758+
case "K":
84759+
result <<= 10;
84760+
case "M":
84761+
result <<= 20;
84762+
case "G":
84763+
result <<= 30;
84764+
default:
84765+
result = NaN;
84766+
}
84767+
}
84768+
else {
84769+
result = NaN;
84770+
break;
84771+
}
84772+
}
84773+
}
84774+
return isNaN(result) ? undefined : { input, value: result };
84775+
}
8473884776
function getInputAsInt(name, options) {
8473984777
const value = parseInt(core.getInput(name, options));
8474084778
if (isNaN(value) || value < 0) {

0 commit comments

Comments
 (0)