Skip to content

Commit 17b6557

Browse files
authored
Merge pull request #12275 from andrewhamon/ah/set-priority-nix-env-install
nix-env: add a --priority flag to --install
2 parents f36cbee + 6ea339c commit 17b6557

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

doc/manual/source/command-ref/nix-env/install.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
[`--from-profile` *path*]
1212
[`--preserve-installed` | `-P`]
1313
[`--remove-all` | `-r`]
14+
[`--priority` *priority*]
1415

1516
# Description
1617

@@ -61,6 +62,10 @@ The arguments *args* map to store paths in a number of possible ways:
6162
The derivations returned by those function calls are installed.
6263
This allows derivations to be specified in an unambiguous way, which is necessary if there are multiple derivations with the same name.
6364

65+
- If `--priority` *priority* is given, the priority of the derivations being installed is set to *priority*.
66+
This can be used to override the priority of the derivations being installed.
67+
This is useful if *args* are [store paths], which don't have any priority information.
68+
6469
- If *args* are [store derivations](@docroot@/glossary.md#gloss-store-derivation), then these are [realised], and the resulting output paths are installed.
6570

6671
- If *args* are [store paths] that are not store derivations, then these are [realised] and installed.
@@ -235,4 +240,3 @@ channel:
235240
```console
236241
$ nix-env --file https://github.com/NixOS/nixpkgs/archive/nixos-14.12.tar.gz --install --attr firefox
237242
```
238-

src/nix-env/nix-env.cc

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,17 @@ static bool keep(PackageInfo & drv)
501501
return drv.queryMetaBool("keep", false);
502502
}
503503

504+
static void setMetaFlag(EvalState & state, PackageInfo & drv,
505+
const std::string & name, const std::string & value)
506+
{
507+
auto v = state.allocValue();
508+
v->mkString(value);
509+
drv.setMeta(name, v);
510+
}
511+
504512

505513
static void installDerivations(Globals & globals,
506-
const Strings & args, const Path & profile)
514+
const Strings & args, const Path & profile, std::optional<int> priority)
507515
{
508516
debug("installing derivations");
509517

@@ -527,6 +535,11 @@ static void installDerivations(Globals & globals,
527535
newNames.insert(DrvName(i.queryName()).name);
528536
}
529537

538+
if (priority) {
539+
for (auto & drv : newElems) {
540+
setMetaFlag(*globals.state, drv, "priority", std::to_string((priority.value())));
541+
}
542+
}
530543

531544
while (true) {
532545
auto lockToken = optimisticLockProfile(profile);
@@ -564,17 +577,25 @@ static void installDerivations(Globals & globals,
564577

565578
static void opInstall(Globals & globals, Strings opFlags, Strings opArgs)
566579
{
580+
std::optional<int> priority;
567581
for (Strings::iterator i = opFlags.begin(); i != opFlags.end(); ) {
568582
auto arg = *i++;
569583
if (parseInstallSourceOptions(globals, i, opFlags, arg)) ;
570584
else if (arg == "--preserve-installed" || arg == "-P")
571585
globals.preserveInstalled = true;
572586
else if (arg == "--remove-all" || arg == "-r")
573587
globals.removeAll = true;
588+
else if (arg == "--priority") {
589+
if (i == opFlags.end())
590+
throw UsageError("'%1%' requires an argument", arg);
591+
priority = string2Int<int>(*i++);
592+
if (!priority)
593+
throw UsageError("'--priority' requires an integer argument");
594+
}
574595
else throw UsageError("unknown flag '%1%'", arg);
575596
}
576597

577-
installDerivations(globals, opArgs, globals.profile);
598+
installDerivations(globals, opArgs, globals.profile, priority);
578599
}
579600

580601

@@ -689,15 +710,6 @@ static void opUpgrade(Globals & globals, Strings opFlags, Strings opArgs)
689710
}
690711

691712

692-
static void setMetaFlag(EvalState & state, PackageInfo & drv,
693-
const std::string & name, const std::string & value)
694-
{
695-
auto v = state.allocValue();
696-
v->mkString(value);
697-
drv.setMeta(name, v);
698-
}
699-
700-
701713
static void opSetFlag(Globals & globals, Strings opFlags, Strings opArgs)
702714
{
703715
if (opFlags.size() > 0)
@@ -1507,7 +1519,8 @@ static int main_nix_env(int argc, char * * argv)
15071519
opFlags.push_back(*arg);
15081520
/* FIXME: hacky */
15091521
if (*arg == "--from-profile" ||
1510-
(op == opQuery && (*arg == "--attr" || *arg == "-A")))
1522+
(op == opQuery && (*arg == "--attr" || *arg == "-A")) ||
1523+
(op == opInstall && (*arg == "--priority")))
15111524
opFlags.push_back(getArg(*arg, arg, end));
15121525
}
15131526
else

tests/functional/user-envs-test-case.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,21 @@ nix-env -q '*' | grepQuiet bar-0.1.1
173173

174174
# Test priorities: foo-0.1 has a lower priority than foo-1.0, so it
175175
# should be possible to install both without a collision. Also test
176-
# ‘--set-flag priority’ to manually override the declared priorities.
176+
# '-i --priority' and '--set-flag priority' to manually override the
177+
# declared priorities.
177178
nix-env -e '*'
178179
nix-env -i foo-0.1 foo-1.0
179180
[ "$($profiles/test/bin/foo)" = "foo-1.0" ]
180181
nix-env --set-flag priority 1 foo-0.1
181182
[ "$($profiles/test/bin/foo)" = "foo-0.1" ]
182183

184+
# Priorities can be overridden with the --priority flag
185+
nix-env -e '*'
186+
nix-env -i foo-1.0
187+
[ "$($profiles/test/bin/foo)" = "foo-1.0" ]
188+
nix-env -i --priority 1 foo-0.1
189+
[ "$($profiles/test/bin/foo)" = "foo-0.1" ]
190+
183191
# Test nix-env --set.
184192
nix-env --set $outPath10
185193
[ "$(nix-store -q --resolve $profiles/test)" = $outPath10 ]

0 commit comments

Comments
 (0)