From 13812a82b603a9a482ac4e0a2f56a172eaf0c288 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 3 Mar 2025 13:10:08 +0100 Subject: [PATCH] Imply --offline during tab completion Previously, if you don't have Internet connectivity, tab completion might try to fetch the flake registry, e.g. $ NIX_GET_COMPLETIONS=4 nix build -vvvvv --offline /home/eelco/De evaluating file '' downloading 'https://channels.nixos.org/flake-registry.json'... warning: error: unable to download 'https://channels.nixos.org/flake-registry.json': Could not resolve hostname (6) Could not resolve host: channels.nixos.org; retrying in 294 ms warning: error: unable to download 'https://channels.nixos.org/flake-registry.json': Could not resolve hostname (6) Could not resolve host: channels.nixos.org; retrying in 541 ms warning: error: unable to download 'https://channels.nixos.org/flake-registry.json': Could not resolve hostname (6) Could not resolve host: channels.nixos.org; retrying in 1230 ms warning: error: unable to download 'https://channels.nixos.org/flake-registry.json': Could not resolve hostname (6) Could not resolve host: channels.nixos.org; retrying in 2285 ms warning: error: unable to download 'https://channels.nixos.org/flake-registry.json': Could not resolve hostname (6) Could not resolve host: channels.nixos.org; using cached version --- src/libutil/args.cc | 3 ++- src/nix/main.cc | 34 ++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/libutil/args.cc b/src/libutil/args.cc index 05ecf724e..54d06125f 100644 --- a/src/libutil/args.cc +++ b/src/libutil/args.cc @@ -236,7 +236,8 @@ void ParseQuoted::operator()(std::shared_ptr &state, Strings & r) { assert(false); } -Strings parseShebangContent(std::string_view s) { +Strings parseShebangContent(std::string_view s) +{ Strings result; std::shared_ptr parserState(std::make_shared(ParseUnquoted(s))); diff --git a/src/nix/main.cc b/src/nix/main.cc index c5e9c0e7f..9155c88de 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -92,6 +92,19 @@ static bool haveInternet() #endif } +static void disableNet() +{ + // FIXME: should check for command line overrides only. + if (!settings.useSubstitutes.overridden) + settings.useSubstitutes = false; + if (!settings.tarballTtl.overridden) + settings.tarballTtl = std::numeric_limits::max(); + if (!fileTransferSettings.tries.overridden) + fileTransferSettings.tries = 0; + if (!fileTransferSettings.connectTimeout.overridden) + fileTransferSettings.connectTimeout = 1; +} + std::string programPath; struct NixArgs : virtual MultiCommand, virtual MixCommonArgs, virtual RootArgs @@ -479,10 +492,16 @@ void mainWrapped(int argc, char * * argv) } }); + if (getEnv("NIX_GET_COMPLETIONS")) + /* Avoid fetching stuff during tab completion. We have to this + early because we haven't checked `haveInternet()` yet + (below). */ + disableNet(); + try { auto isNixCommand = std::regex_search(programName, std::regex("nix$")); auto allowShebang = isNixCommand && argc > 1; - args.parseCmdline(argvToStrings(argc, argv),allowShebang); + args.parseCmdline(argvToStrings(argc, argv), allowShebang); } catch (UsageError &) { if (!args.helpRequested && !args.completions) throw; } @@ -519,17 +538,8 @@ void mainWrapped(int argc, char * * argv) args.useNet = false; } - if (!args.useNet) { - // FIXME: should check for command line overrides only. - if (!settings.useSubstitutes.overridden) - settings.useSubstitutes = false; - if (!settings.tarballTtl.overridden) - settings.tarballTtl = std::numeric_limits::max(); - if (!fileTransferSettings.tries.overridden) - fileTransferSettings.tries = 0; - if (!fileTransferSettings.connectTimeout.overridden) - fileTransferSettings.connectTimeout = 1; - } + if (!args.useNet) + disableNet(); if (args.refresh) { settings.tarballTtl = 0;