Skip to content

Commit

Permalink
Build a minimized Nix with MinGW
Browse files Browse the repository at this point in the history
At this point many features are stripped out, but this works:

- Can run libnix{util,store,expr} unit tests
- Can run some Nix commands

Co-Authored-By volth <[email protected]>
Co-Authored-By Brian McKenna <[email protected]>
  • Loading branch information
Ericson2314 committed Mar 29, 2024
1 parent f68b59f commit 4c49160
Show file tree
Hide file tree
Showing 122 changed files with 1,216 additions and 157 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ clean-files += $(buildprefix)Makefile.config

# List makefiles

include mk/platform.mk

ifeq ($(ENABLE_BUILD), yes)
makefiles = \
mk/precompiled-headers.mk \
Expand All @@ -17,7 +19,10 @@ makefiles = \
src/libmain/local.mk \
src/libexpr/local.mk \
src/libcmd/local.mk \
src/nix/local.mk \
src/nix/local.mk

ifdef HOST_UNIX
makefiles += \
src/resolve-system-dependencies/local.mk \
scripts/local.mk \
misc/bash/local.mk \
Expand All @@ -27,6 +32,7 @@ makefiles = \
misc/launchd/local.mk \
misc/upstart/local.mk
endif
endif

ifeq ($(ENABLE_UNIT_TESTS), yes)
makefiles += \
Expand All @@ -39,6 +45,7 @@ makefiles += \
endif

ifeq ($(ENABLE_FUNCTIONAL_TESTS), yes)
ifdef HOST_UNIX
makefiles += \
tests/functional/local.mk \
tests/functional/ca/local.mk \
Expand All @@ -47,6 +54,7 @@ makefiles += \
tests/functional/test-libstoreconsumer/local.mk \
tests/functional/plugins/local.mk
endif
endif

# Some makefiles require access to built programs and must be included late.
makefiles-late =
Expand All @@ -71,8 +79,6 @@ else
unexport NIX_HARDENING_ENABLE
endif

include mk/platform.mk

ifdef HOST_WINDOWS
# Windows DLLs are stricter about symbol visibility than Unix shared
# objects --- see https://gcc.gnu.org/wiki/Visibility for details.
Expand Down
8 changes: 5 additions & 3 deletions m4/gcc_bug_80431.m4
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ AC_DEFUN([ENSURE_NO_GCC_BUG_80431],
]])],
[status_80431=0],
[status_80431=$?],
[
# Assume we're bug-free when cross-compiling
])
[status_80431=''])
AC_LANG_POP(C++)
AS_CASE([$status_80431],
[''],[
AC_MSG_RESULT(cannot check because cross compiling)
AC_MSG_NOTICE(assume we are bug free)
],
[0],[
AC_MSG_RESULT(yes)
],
Expand Down
21 changes: 12 additions & 9 deletions precompiled-headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,22 @@
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <netdb.h>
#include <pwd.h>
#include <signal.h>
#include <sys/resource.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <termios.h>
#include <unistd.h>

#ifndef _WIN32
# include <grp.h>
# include <netdb.h>
# include <pwd.h>
# include <sys/resource.h>
# include <sys/select.h>
# include <sys/socket.h>
# include <sys/utsname.h>
# include <sys/wait.h>
# include <termios.h>
#endif

#include <nlohmann/json.hpp>
2 changes: 2 additions & 0 deletions src/libcmd/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,10 @@ void MixEnvironment::setEnviron() {
if (!keep.empty())
throw UsageError("--keep does not make sense without --ignore-environment");

#ifndef _WIN32
for (const auto & var : unset)
unsetenv(var.c_str());
#endif
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcmd/common-eval-args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
v->mkString(arg.s);
},
[&](const AutoArgFile & arg) {
v->mkString(readFile(arg.path));
v->mkString(readFile(arg.path.string()));
},
[&](const AutoArgStdin & arg) {
v->mkString(readFile(STDIN_FILENO));
Expand Down
2 changes: 1 addition & 1 deletion src/libcmd/markdown.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include "finally.hh"
#include "terminal.hh"

#include <sys/queue.h>
#if HAVE_LOWDOWN
#include <sys/queue.h>
#include <lowdown.h>
#endif

Expand Down
4 changes: 4 additions & 0 deletions src/libcmd/repl-interacter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ static constexpr const char * promptForType(ReplPromptType promptType)

bool ReadlineLikeInteracter::getLine(std::string & input, ReplPromptType promptType)
{
#ifndef _WIN32
struct sigaction act, old;
sigset_t savedSignalMask, set;

Expand All @@ -161,9 +162,12 @@ bool ReadlineLikeInteracter::getLine(std::string & input, ReplPromptType promptT
};

setupSignals();
#endif
char * s = readline(promptForType(promptType));
Finally doFree([&]() { free(s); });
#ifndef _WIN32
restoreSignals();
#endif

if (g_signal_received) {
g_signal_received = 0;
Expand Down
8 changes: 8 additions & 0 deletions src/libcmd/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,14 @@ void runNix(Path program, const Strings & args,
auto subprocessEnv = getEnv();
subprocessEnv["NIX_CONFIG"] = globalConfig.toKeyValue();

#ifndef _WIN32
runProgram2(RunOptions {
.program = settings.nixBinDir+ "/" + program,
.args = args,
.environment = subprocessEnv,
.input = input,
});
#endif

return;
}
Expand Down Expand Up @@ -228,8 +230,10 @@ ReplExitStatus NixRepl::mainLoop()
printMsg(lvlError, e.msg());
} catch (Error & e) {
printMsg(lvlError, e.msg());
#ifndef _WIN32
} catch (Interrupted & e) {
printMsg(lvlError, e.msg());
#endif
}

// We handled the current input fully, so we should clear it
Expand Down Expand Up @@ -357,7 +361,9 @@ ProcessLineResult NixRepl::processLine(std::string line)
if (line.empty())
return ProcessLineResult::PromptAgain;

#ifndef _WIN32
_isInterrupted = false;
#endif

std::string command, arg;

Expand Down Expand Up @@ -480,6 +486,7 @@ ProcessLineResult NixRepl::processLine(std::string line)
reloadFiles();
}

#ifndef _WIN32
else if (command == ":e" || command == ":edit") {
Value v;
evalString(arg, v);
Expand Down Expand Up @@ -514,6 +521,7 @@ ProcessLineResult NixRepl::processLine(std::string line)
state->resetFileCache();
reloadFiles();
}
#endif

else if (command == ":t") {
Value v;
Expand Down
2 changes: 2 additions & 0 deletions src/libexpr/eval-settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ Strings EvalSettings::getDefaultNixPath()

if (!evalSettings.restrictEval && !evalSettings.pureEval) {
add(getNixDefExpr() + "/channels");
#ifndef _WIN32
add(rootChannelsDir() + "/nixpkgs", "nixpkgs");
add(rootChannelsDir());
#endif
}

return res;
Expand Down
10 changes: 8 additions & 2 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@
#include <optional>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <fstream>
#include <functional>
#include <iostream>

#include <sys/resource.h>
#include <nlohmann/json.hpp>
#include <boost/container/small_vector.hpp>

#ifndef _WIN32
# include <sys/resource.h>
#endif

#if HAVE_BOEHMGC

#define GC_INCLUDE_NEW
Expand Down Expand Up @@ -2633,9 +2635,11 @@ void EvalState::maybePrintStats()

void EvalState::printStatistics()
{
#ifndef _WIN32
struct rusage buf;
getrusage(RUSAGE_SELF, &buf);
float cpuTime = buf.ru_utime.tv_sec + ((float) buf.ru_utime.tv_usec / 1000000);
#endif

uint64_t bEnvs = nrEnvs * sizeof(Env) + nrValuesInEnvs * sizeof(Value *);
uint64_t bLists = nrListElems * sizeof(Value *);
Expand All @@ -2652,7 +2656,9 @@ void EvalState::printStatistics()
if (outPath != "-")
fs.open(outPath, std::fstream::out);
json topObj = json::object();
#ifndef _WIN32
topObj["cpuTime"] = cpuTime;
#endif
topObj["envs"] = {
{"number", nrEnvs},
{"elements", nrValuesInEnvs},
Expand Down
2 changes: 2 additions & 0 deletions src/libexpr/eval.hh
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ struct DebugTrace {
bool isError;
};

// Don't want Windows function
#undef SearchPath

class EvalState : public std::enable_shared_from_this<EvalState>
{
Expand Down
13 changes: 11 additions & 2 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "path-references.hh"
#include "store-api.hh"
#include "util.hh"
#include "processes.hh"
#include "value-to-json.hh"
#include "value-to-xml.hh"
#include "primops.hh"
Expand All @@ -28,7 +27,11 @@
#include <algorithm>
#include <cstring>
#include <regex>
#include <dlfcn.h>

#ifndef _WIN32
# include <dlfcn.h>
# include "processes.hh"
#endif

#include <cmath>

Expand Down Expand Up @@ -324,6 +327,8 @@ static RegisterPrimOp primop_import({
}
});

#ifndef _WIN32

/* Want reasonable symbol names, so extern C */
/* !!! Should we pass the Pos or the file name too? */
extern "C" typedef void (*ValueInitializer)(EvalState & state, Value & v);
Expand Down Expand Up @@ -396,6 +401,8 @@ void prim_exec(EvalState & state, const PosIdx pos, Value * * args, Value & v)
}
}

#endif

/* Return a string representing the type of the expression. */
static void prim_typeOf(EvalState & state, const PosIdx pos, Value * * args, Value & v)
{
Expand Down Expand Up @@ -4576,6 +4583,7 @@ void EvalState::createBaseEnv()
)",
});

#ifndef _WIN32
// Miscellaneous
if (evalSettings.enableNativeCode) {
addPrimOp({
Expand All @@ -4589,6 +4597,7 @@ void EvalState::createBaseEnv()
.fun = prim_exec,
});
}
#endif

addPrimOp({
.name = "__traceVerbose",
Expand Down
3 changes: 3 additions & 0 deletions src/libexpr/search-path.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace nix {

// Do not want the windows macro (alias to `SearchPathA`)
#undef SearchPath

/**
* A "search path" is a list of ways look for something, used with
* `builtins.findFile` and `< >` lookup expressions.
Expand Down
Loading

0 comments on commit 4c49160

Please sign in to comment.