Skip to content

Commit

Permalink
Improve the nix.conf loading logic when using daemon mode
Browse files Browse the repository at this point in the history
This now matches what the nix client does.
  • Loading branch information
sandydoo committed Oct 24, 2023
1 parent 6101e56 commit b235119
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
36 changes: 25 additions & 11 deletions dist/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7864,17 +7864,20 @@ async function setup() {
// Otherwise it will look for nix / nix.conf files in XDG_CONFIG_DIRS and XDG_CONFIG_HOME.If
// unset, XDG_CONFIG_DIRS defaults to / etc / xdg, and XDG_CONFIG_HOME defaults to $HOME /.config
// as per XDG Base Directory Specification.
const userConfFiles = process.env['NIX_USER_CONF_FILES'] ?? '';
const xdgConfigHome = process.env['XDG_CONFIG_HOME'] ?? `${os.homedir()}/.config`;
const xdgConfigDirs = process.env['XDG_CONFIG_DIRS'] ?? '/etc/xdg';
const nixConfDir = process.env['NIX_CONF_DIR'] ?? '/etc/nix';
core.exportVariable('NIX_USER_CONF_FILES', [
postBuildHookConfigPath,
userConfFiles,
`${xdgConfigHome}/nix/nix.conf`,
`${xdgConfigDirs}/nix/nix.conf`,
`${nixConfDir}/nix.conf`,
].filter((x) => x !== '').join(':'));
//
// The system nix.conf ($NIX_CONF_DIR/nix.conf) is always loaded first.
//
// If the user has overridden the default nix.conf locations with NIX_USER_CONF_DIR, we reuse it and prepend out own config.
const existingUserConfEnv = process.env['NIX_USER_CONF_FILES'] ?? '';
let nixUserConfFilesEnv = '';
if (existingUserConfEnv) {
nixUserConfFilesEnv = postBuildHookConfigPath + ':' + existingUserConfEnv;
}
else {
const userConfigFiles = getUserConfigFiles();
nixUserConfFilesEnv = [postBuildHookConfigPath, ...userConfigFiles].filter((x) => x !== '').join(':');
}
core.exportVariable('NIX_USER_CONF_FILES', nixUserConfFilesEnv);
core.debug(`Registered post-build-hook nix config in NIX_USER_CONF_FILES=${process.env['NIX_USER_CONF_FILES']}`);
// Expose the daemon directory for the post action hook
core.exportVariable(ENV_CACHIX_DAEMON_DIR, daemonDir);
Expand Down Expand Up @@ -7939,6 +7942,17 @@ async function upload() {
function pidFilePath(daemonDir) {
return path.join(daemonDir, 'daemon.pid');
}
// Get the paths to the user config files.
function getUserConfigFiles() {
const userConfigDirs = getUserConfigDirs();
return userConfigDirs.map((dir) => `${dir}/nix/nix.conf`);
}
// Get the user config directories.
function getUserConfigDirs() {
const xdgConfigHome = process.env['XDG_CONFIG_HOME'] ?? `${os.homedir()}/.config`;
const xdgConfigDirs = (process.env['XDG_CONFIG_DIRS'] ?? '/etc/xdg').split(':');
return [xdgConfigHome, ...xdgConfigDirs];
}
const isPost = !!core.getState('isPost');
// Main
if (!isPost) {
Expand Down
40 changes: 28 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,23 @@ async function setup() {
// Otherwise it will look for nix / nix.conf files in XDG_CONFIG_DIRS and XDG_CONFIG_HOME.If
// unset, XDG_CONFIG_DIRS defaults to / etc / xdg, and XDG_CONFIG_HOME defaults to $HOME /.config
// as per XDG Base Directory Specification.
const userConfFiles = process.env['NIX_USER_CONF_FILES'] ?? '';
const xdgConfigHome = process.env['XDG_CONFIG_HOME'] ?? `${os.homedir()}/.config`;
const xdgConfigDirs = process.env['XDG_CONFIG_DIRS'] ?? '/etc/xdg';
const nixConfDir = process.env['NIX_CONF_DIR'] ?? '/etc/nix';
//
// The system nix.conf ($NIX_CONF_DIR/nix.conf) is always loaded first.
//
// If the user has overridden the default nix.conf locations with NIX_USER_CONF_DIR, we reuse it and prepend out own config.
const existingUserConfEnv = process.env['NIX_USER_CONF_FILES'] ?? '';
let nixUserConfFilesEnv = '';

if (existingUserConfEnv) {
nixUserConfFilesEnv = postBuildHookConfigPath + ':' + existingUserConfEnv;
} else {
const userConfigFiles = getUserConfigFiles();
nixUserConfFilesEnv = [postBuildHookConfigPath, ...userConfigFiles].filter((x) => x !== '').join(':');
}

core.exportVariable(
'NIX_USER_CONF_FILES',
[
postBuildHookConfigPath,
userConfFiles,
`${xdgConfigHome}/nix/nix.conf`,
`${xdgConfigDirs}/nix/nix.conf`,
`${nixConfDir}/nix.conf`,
].filter((x) => x !== '').join(':')
nixUserConfFilesEnv,
);
core.debug(`Registered post-build-hook nix config in NIX_USER_CONF_FILES=${process.env['NIX_USER_CONF_FILES']}`);

Expand Down Expand Up @@ -245,10 +248,23 @@ async function upload() {
core.endGroup();
}

function pidFilePath(daemonDir: string) {
function pidFilePath(daemonDir: string): string {
return path.join(daemonDir, 'daemon.pid');
}

// Get the paths to the user config files.
function getUserConfigFiles(): string[] {
const userConfigDirs = getUserConfigDirs();
return userConfigDirs.map((dir) => `${dir}/nix/nix.conf`);
}

// Get the user config directories.
function getUserConfigDirs(): string[] {
const xdgConfigHome = process.env['XDG_CONFIG_HOME'] ?? `${os.homedir()}/.config`;
const xdgConfigDirs = (process.env['XDG_CONFIG_DIRS'] ?? '/etc/xdg').split(':');
return [xdgConfigHome, ...xdgConfigDirs];
}

const isPost = !!core.getState('isPost');

// Main
Expand Down

0 comments on commit b235119

Please sign in to comment.