Skip to content

Commit f9bf9fc

Browse files
committed
detect nix build
1 parent 2b7b369 commit f9bf9fc

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/gb/gb.h

+5
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ extern "C" {
278278
#include <semaphore.h>
279279
#endif
280280

281+
#if defined(GB_SYSTEM_LINUX)
282+
#include <grp.h>
283+
#include <pwd.h>
284+
#endif
285+
281286

282287
////////////////////////////////////////////////////////////////
283288
//

src/linker.cpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,23 @@ try_cross_linking:;
439439
#if !defined(GB_SYSTEM_WINDOWS)
440440
lib_str = gb_string_appendc(lib_str, "-L/ ");
441441
#endif
442+
443+
// Check if Odin is called inside Nix build environment.
444+
bool nix_build = false;
445+
#if defined(GB_SYSTEM_LINUX)
446+
__uid_t uid = getuid();
447+
passwd *passw = getpwuid(uid);
448+
if (passw != NULL) {
449+
int groups_len = 0;
450+
getgrouplist(passw->pw_name, passw->pw_gid, NULL, &groups_len);
451+
if (groups_len == 1) {
452+
__gid_t groups[2];
453+
getgrouplist(passw->pw_name, passw->pw_gid, groups, &groups_len);
454+
const char *gr_name = getgrgid(groups[0])->gr_name;
455+
nix_build = gb_strcmp(gr_name, "nixbld") == 0;
456+
}
457+
}
458+
#endif
442459

443460
StringSet asm_files = {};
444461
string_set_init(&asm_files, 64);
@@ -615,7 +632,12 @@ try_cross_linking:;
615632
// local to the executable (unless the system collection is used, in which case we search
616633
// the system library paths for the library file).
617634
if (string_ends_with(lib, str_lit(".a")) || string_ends_with(lib, str_lit(".o")) || string_ends_with(lib, str_lit(".so")) || string_contains_string(lib, str_lit(".so."))) {
618-
lib_str = gb_string_append_fmt(lib_str, " \"%.*s\" ", LIT(lib));
635+
// Inside Nix build environment, the linker will not be able to find the libraries if `-l:` is specified.
636+
if (nix_build) {
637+
lib_str = gb_string_append_fmt(lib_str, " \"%.*s\" ", LIT(lib));
638+
} else {
639+
lib_str = gb_string_append_fmt(lib_str, " -l:\"%.*s\" ", LIT(lib));
640+
}
619641
} else {
620642
// dynamic or static system lib, just link regularly searching system library paths
621643
lib_str = gb_string_append_fmt(lib_str, " -l%.*s ", LIT(lib));

0 commit comments

Comments
 (0)