Skip to content

Commit 8c903e0

Browse files
committed
nix-flake-c: Add lock flags
Going with a slightly more limited, high level API supporting the three main use cases. This should allow the underlying code to evolve more freely.
1 parent 1a3789e commit 8c903e0

File tree

3 files changed

+328
-26
lines changed

3 files changed

+328
-26
lines changed

src/libflake-c/nix_api_flake.cc

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,16 @@ nix_flake_lock_flags * nix_flake_lock_flags_new(nix_c_context * context, nix_fla
9797
{
9898
nix_clear_err(context);
9999
try {
100-
auto lockSettings = nix::make_ref<nix::flake::LockFlags>();
100+
auto lockSettings = nix::make_ref<nix::flake::LockFlags>(nix::flake::LockFlags{
101+
.recreateLockFile = false,
102+
.updateLockFile = true, // == `nix_flake_lock_flags_set_mode_write_as_needed`
103+
.writeLockFile = true, // == `nix_flake_lock_flags_set_mode_write_as_needed`
104+
.failOnUnlocked = false, // == `nix_flake_lock_flags_set_mode_write_as_needed`
105+
.useRegistries = false,
106+
.allowUnlocked = false, // == `nix_flake_lock_flags_set_mode_write_as_needed`
107+
.commitLockFile = false,
108+
109+
});
101110
return new nix_flake_lock_flags{lockSettings};
102111
}
103112
NIXC_CATCH_ERRS_NULL
@@ -108,16 +117,68 @@ void nix_flake_lock_flags_free(nix_flake_lock_flags * flags)
108117
delete flags;
109118
}
110119

120+
nix_err nix_flake_lock_flags_set_mode_virtual(nix_c_context * context, nix_flake_lock_flags * flags)
121+
{
122+
nix_clear_err(context);
123+
try {
124+
flags->lockFlags->updateLockFile = true;
125+
flags->lockFlags->writeLockFile = false;
126+
flags->lockFlags->failOnUnlocked = false;
127+
flags->lockFlags->allowUnlocked = true;
128+
}
129+
NIXC_CATCH_ERRS
130+
}
131+
132+
nix_err nix_flake_lock_flags_set_mode_write_as_needed(nix_c_context * context, nix_flake_lock_flags * flags)
133+
{
134+
nix_clear_err(context);
135+
try {
136+
flags->lockFlags->updateLockFile = true;
137+
flags->lockFlags->writeLockFile = true;
138+
flags->lockFlags->failOnUnlocked = false;
139+
flags->lockFlags->allowUnlocked = true;
140+
}
141+
NIXC_CATCH_ERRS
142+
}
143+
144+
nix_err nix_flake_lock_flags_set_mode_check(nix_c_context * context, nix_flake_lock_flags * flags)
145+
{
146+
nix_clear_err(context);
147+
try {
148+
flags->lockFlags->updateLockFile = false;
149+
flags->lockFlags->writeLockFile = false;
150+
flags->lockFlags->failOnUnlocked = true;
151+
flags->lockFlags->allowUnlocked = false;
152+
}
153+
NIXC_CATCH_ERRS
154+
}
155+
156+
nix_err nix_flake_lock_flags_add_input_override(
157+
nix_c_context * context, nix_flake_lock_flags * flags, const char * inputPath, nix_flake_reference * flakeRef)
158+
{
159+
nix_clear_err(context);
160+
try {
161+
auto path = nix::flake::parseInputAttrPath(inputPath);
162+
flags->lockFlags->inputOverrides.emplace(path, *flakeRef->flakeRef);
163+
if (flags->lockFlags->writeLockFile) {
164+
return nix_flake_lock_flags_set_mode_virtual(context, flags);
165+
}
166+
}
167+
NIXC_CATCH_ERRS
168+
}
169+
111170
nix_locked_flake * nix_flake_lock(
112171
nix_c_context * context,
113-
nix_flake_settings * settings,
172+
nix_fetchers_settings * fetchSettings,
173+
nix_flake_settings * flakeSettings,
114174
EvalState * eval_state,
115175
nix_flake_lock_flags * flags,
116176
nix_flake_reference * flakeReference)
117177
{
178+
nix_clear_err(context);
118179
try {
119180
auto lockedFlake = nix::make_ref<nix::flake::LockedFlake>(nix::flake::lockFlake(
120-
*settings->settings, eval_state->state, *flakeReference->flakeRef, *flags->lockFlags));
181+
*flakeSettings->settings, eval_state->state, *flakeReference->flakeRef, *flags->lockFlags));
121182
return new nix_locked_flake{lockedFlake};
122183
}
123184
NIXC_CATCH_ERRS_NULL

src/libflake-c/nix_api_flake.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,49 @@ nix_flake_lock_flags * nix_flake_lock_flags_new(nix_c_context * context, nix_fla
126126
*/
127127
void nix_flake_lock_flags_free(nix_flake_lock_flags * settings);
128128

129+
/**
130+
* @brief Put the lock flags in a mode that checks whether the lock is up to date.
131+
* @param[out] context Optional, stores error information
132+
* @param[in] flags The flags to modify
133+
* @return NIX_OK on success, NIX_ERR on failure
134+
*
135+
* This causes `nix_flake_lock` to fail if the lock needs to be updated.
136+
*/
137+
nix_err nix_flake_lock_flags_set_mode_check(nix_c_context * context, nix_flake_lock_flags * flags);
138+
139+
/**
140+
* @brief Put the lock flags in a mode that updates the lock file in memory, if needed.
141+
* @param[out] context Optional, stores error information
142+
* @param[in] flags The flags to modify
143+
* @param[in] update Whether to allow updates
144+
*
145+
* This will cause `nix_flake_lock` to update the lock file in memory, if needed.
146+
*/
147+
nix_err nix_flake_lock_flags_set_mode_virtual(nix_c_context * context, nix_flake_lock_flags * flags);
148+
149+
/**
150+
* @brief Put the lock flags in a mode that updates the lock file on disk, if needed.
151+
* @param[out] context Optional, stores error information
152+
* @param[in] flags The flags to modify
153+
* @param[in] update Whether to allow updates
154+
*
155+
* This will cause `nix_flake_lock` to update the lock file on disk, if needed.
156+
*/
157+
nix_err nix_flake_lock_flags_set_mode_write_as_needed(nix_c_context * context, nix_flake_lock_flags * flags);
158+
159+
/**
160+
* @brief Add input overrides to the lock flags
161+
* @param[out] context Optional, stores error information
162+
* @param[in] flags The flags to modify
163+
* @param[in] inputPath The input path to override
164+
* @param[in] flakeRef The flake reference to use as the override
165+
*
166+
* This switches the `flags` to `nix_flake_lock_flags_set_mode_virtual` if not in mode
167+
* `nix_flake_lock_flags_set_mode_check`.
168+
*/
169+
nix_err nix_flake_lock_flags_add_input_override(
170+
nix_c_context * context, nix_flake_lock_flags * flags, const char * inputPath, nix_flake_reference * flakeRef);
171+
129172
/**
130173
* @brief Lock a flake, if not already locked.
131174
* @param[out] context Optional, stores error information
@@ -135,6 +178,7 @@ void nix_flake_lock_flags_free(nix_flake_lock_flags * settings);
135178
*/
136179
nix_locked_flake * nix_flake_lock(
137180
nix_c_context * context,
181+
nix_fetchers_settings * fetchSettings,
138182
nix_flake_settings * settings,
139183
EvalState * eval_state,
140184
nix_flake_lock_flags * flags,

0 commit comments

Comments
 (0)