home-manager: fix GC issue
It was previously possible to create the news information and lose it in a Nix GC before being able to view it. This also causes a switch to error out. This change makes the news information a root in the garbage collector. Note, this change also removes the need for `nix eval` so the `doBuildAttr` function is simplified accordingly. Fixes #327
This commit is contained in:
parent
30cba446f2
commit
4f67e8d0c3
1 changed files with 32 additions and 26 deletions
|
@ -12,6 +12,14 @@ function errorEcho() {
|
||||||
echo $* >&2
|
echo $* >&2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setWorkDir() {
|
||||||
|
if [[ ! -v WORK_DIR ]]; then
|
||||||
|
WORK_DIR="$(mktemp --tmpdir -d home-manager-build.XXXXXXXXXX)"
|
||||||
|
# shellcheck disable=2064
|
||||||
|
trap "rm -r '$WORK_DIR'" EXIT
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
# Attempts to set the HOME_MANAGER_CONFIG global variable.
|
# Attempts to set the HOME_MANAGER_CONFIG global variable.
|
||||||
#
|
#
|
||||||
# If no configuration file can be found then this function will print
|
# If no configuration file can be found then this function will print
|
||||||
|
@ -58,8 +66,6 @@ function doBuildAttr() {
|
||||||
setConfigFile
|
setConfigFile
|
||||||
setHomeManagerNixPath
|
setHomeManagerNixPath
|
||||||
|
|
||||||
local subCommand="$1"
|
|
||||||
shift
|
|
||||||
local extraArgs="$*"
|
local extraArgs="$*"
|
||||||
|
|
||||||
for p in "${EXTRA_NIX_PATH[@]}"; do
|
for p in "${EXTRA_NIX_PATH[@]}"; do
|
||||||
|
@ -72,18 +78,12 @@ function doBuildAttr() {
|
||||||
|
|
||||||
# shellcheck disable=2086
|
# shellcheck disable=2086
|
||||||
if [[ -v USE_NIX2_COMMAND ]]; then
|
if [[ -v USE_NIX2_COMMAND ]]; then
|
||||||
if [[ $subCommand == 'eval' ]]; then
|
nix build \
|
||||||
extraArgs="$extraArgs --raw"
|
|
||||||
fi
|
|
||||||
nix $subCommand \
|
|
||||||
-f "<home-manager/home-manager/home-manager.nix>" \
|
-f "<home-manager/home-manager/home-manager.nix>" \
|
||||||
$extraArgs \
|
$extraArgs \
|
||||||
--argstr confPath "$HOME_MANAGER_CONFIG" \
|
--argstr confPath "$HOME_MANAGER_CONFIG" \
|
||||||
--argstr confAttr "$HOME_MANAGER_CONFIG_ATTRIBUTE"
|
--argstr confAttr "$HOME_MANAGER_CONFIG_ATTRIBUTE"
|
||||||
else
|
else
|
||||||
if [[ $subCommand == 'eval' ]]; then
|
|
||||||
extraArgs="$extraArgs --no-out-link"
|
|
||||||
fi
|
|
||||||
nix-build \
|
nix-build \
|
||||||
"<home-manager/home-manager/home-manager.nix>" \
|
"<home-manager/home-manager/home-manager.nix>" \
|
||||||
$extraArgs \
|
$extraArgs \
|
||||||
|
@ -143,10 +143,10 @@ function doBuild() {
|
||||||
local exitCode
|
local exitCode
|
||||||
|
|
||||||
if [[ -v USE_NIX2_COMMAND ]]; then
|
if [[ -v USE_NIX2_COMMAND ]]; then
|
||||||
doBuildAttr build activationPackage \
|
doBuildAttr activationPackage \
|
||||||
&& exitCode=0 || exitCode=1
|
&& exitCode=0 || exitCode=1
|
||||||
else
|
else
|
||||||
doBuildAttr build --attr activationPackage \
|
doBuildAttr --attr activationPackage \
|
||||||
&& exitCode=0 || exitCode=1
|
&& exitCode=0 || exitCode=1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -156,37 +156,33 @@ function doBuild() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function doSwitch() {
|
function doSwitch() {
|
||||||
|
setWorkDir
|
||||||
|
|
||||||
local newsInfo
|
local newsInfo
|
||||||
newsInfo=$(buildNews)
|
newsInfo=$(buildNews)
|
||||||
|
|
||||||
local generation
|
local generation
|
||||||
local exitCode=0
|
local exitCode=0
|
||||||
local wrkdir
|
|
||||||
|
|
||||||
# Build the generation and run the activate script. Note, we
|
# Build the generation and run the activate script. Note, we
|
||||||
# specify an output link so that it is treated as a GC root. This
|
# specify an output link so that it is treated as a GC root. This
|
||||||
# prevents an unfortunately timed GC from removing the generation
|
# prevents an unfortunately timed GC from removing the generation
|
||||||
# before activation completes.
|
# before activation completes.
|
||||||
wrkdir="$(mktemp -d home-manager-build.XXXXXXXXXX)"
|
generation="$WORK_DIR/generation"
|
||||||
generation="$wrkdir/result"
|
|
||||||
|
|
||||||
if [[ -v USE_NIX2_COMMAND ]]; then
|
if [[ -v USE_NIX2_COMMAND ]]; then
|
||||||
doBuildAttr build \
|
doBuildAttr \
|
||||||
--out-link "$generation" \
|
--out-link "$generation" \
|
||||||
activationPackage \
|
activationPackage \
|
||||||
&& "$generation/activate" || exitCode=1
|
&& "$generation/activate" || exitCode=1
|
||||||
else
|
else
|
||||||
doBuildAttr build \
|
doBuildAttr \
|
||||||
--out-link "$generation" \
|
--out-link "$generation" \
|
||||||
--no-build-output \
|
--no-build-output \
|
||||||
--attr activationPackage > /dev/null \
|
--attr activationPackage > /dev/null \
|
||||||
&& "$generation/activate" || exitCode=1
|
&& "$generation/activate" || exitCode=1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Because the previous command never fails, the script keeps
|
|
||||||
# running and $wrkdir is always removed.
|
|
||||||
rm -r "$wrkdir"
|
|
||||||
|
|
||||||
presentNews "$newsInfo"
|
presentNews "$newsInfo"
|
||||||
|
|
||||||
return $exitCode
|
return $exitCode
|
||||||
|
@ -267,26 +263,36 @@ function newsReadIdsFile() {
|
||||||
# Builds news meta information to be sourced into this script.
|
# Builds news meta information to be sourced into this script.
|
||||||
#
|
#
|
||||||
# Note, we suppress build output to remove unnecessary verbosity. We
|
# Note, we suppress build output to remove unnecessary verbosity. We
|
||||||
# also use "no out link" to avoid the need for a build directory
|
# put the output in the work directory to avoid the risk of an
|
||||||
# (although this exposes the risk of GC removing the result before we
|
# unfortunately timed GC removing it.
|
||||||
# manage to source it).
|
|
||||||
function buildNews() {
|
function buildNews() {
|
||||||
|
local output
|
||||||
|
output="$WORK_DIR/news-info.sh"
|
||||||
|
|
||||||
if [[ -v USE_NIX2_COMMAND ]]; then
|
if [[ -v USE_NIX2_COMMAND ]]; then
|
||||||
doBuildAttr eval \
|
doBuildAttr \
|
||||||
|
--out-link "$output" \
|
||||||
--quiet \
|
--quiet \
|
||||||
--arg check false \
|
--arg check false \
|
||||||
--argstr newsReadIdsFile "$(newsReadIdsFile)" \
|
--argstr newsReadIdsFile "$(newsReadIdsFile)" \
|
||||||
newsInfo
|
newsInfo
|
||||||
else
|
else
|
||||||
doBuildAttr eval \
|
doBuildAttr \
|
||||||
|
--out-link "$output" \
|
||||||
|
--no-build-output \
|
||||||
--quiet \
|
--quiet \
|
||||||
--arg check false \
|
--arg check false \
|
||||||
--argstr newsReadIdsFile "$(newsReadIdsFile)" \
|
--argstr newsReadIdsFile "$(newsReadIdsFile)" \
|
||||||
--attr newsInfo
|
--attr newsInfo \
|
||||||
|
> /dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
echo "$output"
|
||||||
}
|
}
|
||||||
|
|
||||||
function doShowNews() {
|
function doShowNews() {
|
||||||
|
setWorkDir
|
||||||
|
|
||||||
local infoFile
|
local infoFile
|
||||||
infoFile=$(buildNews) || return 1
|
infoFile=$(buildNews) || return 1
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue