1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 10:41:16 +02:00

Merge pull request #12492 from xokdvium/refactor/move-show-man-page-to-nix-cli

Move code related to NIX_MAN_DIR from libstore to nix-cli
This commit is contained in:
John Ericson 2025-02-17 22:39:46 -05:00 committed by GitHub
commit f55eb06d76
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 79 additions and 25 deletions

View file

@ -315,20 +315,6 @@ void printVersion(const std::string & programName)
throw Exit();
}
void showManPage(const std::string & name)
{
restoreProcessContext();
setEnv("MANPATH", settings.nixManDir.c_str());
execlp("man", "man", name.c_str(), nullptr);
if (errno == ENOENT) {
// Not SysError because we don't want to suffix the errno, aka No such file or directory.
throw Error("The '%1%' command was not found, but it is needed for '%2%' and some other '%3%' commands' help text. Perhaps you could install the '%1%' command?", "man", name.c_str(), "nix-*");
}
throw SysError("command 'man %1%' failed", name.c_str());
}
int handleExceptions(const std::string & programName, std::function<void()> fun)
{
ReceiveInterrupts receiveInterrupts; // FIXME: need better place for this

View file

@ -70,11 +70,6 @@ struct LegacyArgs : public MixCommonArgs, public RootArgs
};
/**
* Show the manual page for the specified program.
*/
void showManPage(const std::string & name);
/**
* The constructor of this class starts a pager if standard output is a
* terminal and $PAGER is set. Standard output is redirected to the

View file

@ -65,7 +65,6 @@ Settings::Settings()
, nixStateDir(canonPath(getEnvNonEmpty("NIX_STATE_DIR").value_or(NIX_STATE_DIR)))
, nixConfDir(canonPath(getEnvNonEmpty("NIX_CONF_DIR").value_or(NIX_CONF_DIR)))
, nixUserConfFiles(getUserConfigFiles())
, nixManDir(canonPath(NIX_MAN_DIR))
, nixDaemonSocketFile(canonPath(getEnvNonEmpty("NIX_DAEMON_SOCKET_PATH").value_or(nixStateDir + DEFAULT_SOCKET_PATH)))
{
#ifndef _WIN32

View file

@ -84,11 +84,6 @@ public:
*/
std::vector<Path> nixUserConfFiles;
/**
* The directory where the man pages are stored.
*/
Path nixManDir;
/**
* File name of the socket the daemon listens to.
*/

View file

@ -28,6 +28,7 @@
#include "users.hh"
#include "network-proxy.hh"
#include "compatibility-settings.hh"
#include "man-pages.hh"
using namespace nix;
using namespace std::string_literals;

View file

@ -8,6 +8,7 @@
#include "users.hh"
#include "tarball.hh"
#include "self-exe.hh"
#include "man-pages.hh"
#include <fcntl.h>
#include <regex>

View file

@ -7,6 +7,7 @@
#include "shared.hh"
#include "globals.hh"
#include "legacy.hh"
#include "man-pages.hh"
#include <iostream>
#include <cerrno>

View file

@ -2,6 +2,7 @@
#include "realisation.hh"
#include "store-api.hh"
#include "legacy.hh"
#include "man-pages.hh"
using namespace nix;

View file

@ -17,6 +17,7 @@
#include "legacy.hh"
#include "eval-settings.hh" // for defexpr
#include "terminal.hh"
#include "man-pages.hh"
#include <cerrno>
#include <ctime>

View file

@ -12,6 +12,7 @@
#include "local-fs-store.hh"
#include "common-eval-args.hh"
#include "legacy.hh"
#include "man-pages.hh"
#include <map>
#include <iostream>

View file

@ -12,6 +12,7 @@
#include "legacy.hh"
#include "posix-source-accessor.hh"
#include "path-with-outputs.hh"
#include "man-pages.hh"
#ifndef _WIN32 // TODO implement on Windows or provide allowed-to-noop interface
# include "local-store.hh"

View file

@ -8,6 +8,7 @@
#include "git.hh"
#include "posix-source-accessor.hh"
#include "misc-store-flags.hh"
#include "man-pages.hh"
using namespace nix;

29
src/nix/man-pages.cc Normal file
View file

@ -0,0 +1,29 @@
#include "man-pages.hh"
#include "file-system.hh"
#include "current-process.hh"
#include "environment-variables.hh"
namespace nix {
std::filesystem::path getNixManDir()
{
return canonPath(NIX_MAN_DIR);
}
void showManPage(const std::string & name)
{
restoreProcessContext();
setEnv("MANPATH", getNixManDir().c_str());
execlp("man", "man", name.c_str(), nullptr);
if (errno == ENOENT) {
// Not SysError because we don't want to suffix the errno, aka No such file or directory.
throw Error(
"The '%1%' command was not found, but it is needed for '%2%' and some other '%3%' commands' help text. Perhaps you could install the '%1%' command?",
"man",
name.c_str(),
"nix-*");
}
throw SysError("command 'man %1%' failed", name.c_str());
}
}

28
src/nix/man-pages.hh Normal file
View file

@ -0,0 +1,28 @@
#pragma once
///@file
#include <filesystem>
#include <string>
namespace nix {
/**
* @brief Get path to the nix manual dir.
*
* Nix relies on the man pages being available at a NIX_MAN_DIR for
* displaying help messaged for legacy cli.
*
* NIX_MAN_DIR is a compile-time parameter, so man pages are unlikely to work
* for cases when the nix executable is installed out-of-store or as a static binary.
*
*/
std::filesystem::path getNixManDir();
/**
* Show the manual page for the specified program.
*
* @param name Name of the man item.
*/
void showManPage(const std::string & name);
}

View file

@ -90,6 +90,7 @@ nix_sources = [config_h] + files(
'ls.cc',
'main.cc',
'make-content-addressed.cc',
'man-pages.cc',
'nar.cc',
'optimise-store.cc',
'path-from-hash-part.cc',
@ -182,6 +183,16 @@ if host_machine.system() != 'windows'
]
endif
fs = import('fs')
prefix = get_option('prefix')
mandir = get_option('mandir')
mandir = fs.is_absolute(mandir) ? mandir : prefix / mandir
cpp_args= [
'-DNIX_MAN_DIR="@0@"'.format(mandir)
]
include_dirs = [include_directories('.')]
this_exe = executable(
@ -189,6 +200,7 @@ this_exe = executable(
sources,
dependencies : deps_private_subproject + deps_private + deps_other,
include_directories : include_dirs,
cpp_args : cpp_args,
link_args: linker_export_flags,
install : true,
)

View file

@ -12,6 +12,7 @@
#include "posix-source-accessor.hh"
#include "misc-store-flags.hh"
#include "terminal.hh"
#include "man-pages.hh"
#include <nlohmann/json.hpp>

View file

@ -15,6 +15,7 @@
#include "finally.hh"
#include "legacy.hh"
#include "daemon.hh"
#include "man-pages.hh"
#include <algorithm>
#include <climits>