Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

language.c: bang into shape #102

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions extra/language/c.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,67 @@ in
with lib;
{
options.language.c = {
compiler = mkOption {
type = strOrPackage;
default = pkgs.clang;
defaultText = "pkgs.clang";
description = ''
Which C compiler to use.

For gcc, use pkgs.gcc-unwrapped.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a wrapped and a unwrapped version will behave quite differently here. i.e. the wrapped version of clang will set hardening flags that the unwrapped version of gcc won't. This might be surprising for users.

'';
};

linker = mkOption {
type = strOrPackage;
default = pkgs.binutils;
defaultText = "pkgs.binutils";
description = "Which linker package to use";
};

libraries = mkOption {
type = types.listOf strOrPackage;
default = [ ];
description = "Use this when another language dependens on a dynamic library";
example = lib.literalExample ''
[ pkgs.glibc ]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does glibc in libraries have any effect when a wrapped clang is used?

'';
};

pkg-config = mkEnableOption "use pkg-config";

includes = mkOption {
type = types.listOf strOrPackage;
default = [ ];
description = "C dependencies from nixpkgs";
};

compiler = mkOption {
type = strOrPackage;
default = pkgs.clang;
defaultText = "pkgs.clang";
description = "Which C compiler to use";
};
};

config = {
devshell.packages =
[ cfg.compiler ]
[ cfg.compiler cfg.linker ]
++
(lib.optionals hasLibraries (map lib.getLib cfg.libraries))
++
# Assume we want pkg-config, because it's good
(lib.optionals hasIncludes ([ pkgs.pkg-config ] ++ (map lib.getDev cfg.includes)))
(lib.optional cfg.pkg-config pkgs.pkg-config)
;

env =
(lib.optionals hasLibraries [
{
name = "LD_LIBRARY_PATH";
prefix = "$DEVSHELL_DIR/lib";
name = "LIBRARY_PATH";
value = lib.concatStringsSep ":" (map (x: "${lib.getLib x}/lib") cfg.libraries);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value = "$DEVSHELL_DIR/lib";

does not work here?

Also how would programs build like this find their libraries at runtime? This is especially a problem when you run outside of devshell.

Copy link
Member

@Mic92 Mic92 Mar 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about setting NIX_LDFLAGS="-rpath $DEVSHELL_DIR/lib";

}
])
++ lib.optionals hasIncludes [
{
name = "LD_INCLUDE_PATH";
prefix = "$DEVSHELL_DIR/include";
}
{
name = "PKG_CONFIG_PATH";
prefix = "$DEVSHELL_DIR/lib/pkgconfig";
}
];
++ (lib.optional hasIncludes {
name = "LD_INCLUDE_PATH";
prefix = "$DEVSHELL_DIR/include";
})
++ (lib.optional cfg.pkg-config {
name = "PKG_CONFIG_PATH";
Copy link
Member

@Mic92 Mic92 Mar 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also want to set CC/CXX/LD. Otherwise many buildsystems will default to something like CC = /usr/bin/gcc

value = lib.concatStringsSep ":" (map (x: "${lib.getLib x}/lib/pkgconfig") cfg.libraries);
})
;
};
}