Skip to content

Instantly share code, notes, and snippets.

@edef1c
Last active January 19, 2016 15:14
Show Gist options
  • Save edef1c/147c075bd92c70e65c31 to your computer and use it in GitHub Desktop.
Save edef1c/147c075bd92c70e65c31 to your computer and use it in GitHub Desktop.
{ stdenv, musl, ... }:
stdenv.mkDerivation {
name = "env-friendly-1.0.0";
unpackPhase = ''
cp -v ${./Makefile} Makefile
cp -v ${./env.c} env.c
'';
CFLAGS = "-isystem ${musl}/include -B${musl}/lib -L${musl}/lib";
LDFLAGS = "-static -Wl,--gc-sections";
meta = with stdenv.lib; {
description = "An environmentally friendly replacement for /usr/bin/env";
license = licenses.cc0;
maintainers = [ "Nathan Zadoks <[email protected]>" ];
platforms = stdenv.lib.platforms.linux;
};
}
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "%s: no command specified\n", argv[0]);
exit(-1);
}
execvp(argv[1], &argv[1]);
char* err = strerror(errno);
fprintf(stderr, "%s: %s: %s", argv[0], argv[1], err);
exit(127);
}
all: env
install: env
install -D env $(out)/bin/env
{ config, pkgs, lib, ... }: with lib; let
env-friendly = callPackage ./. {};
in {
system.activationScripts.usrbinenv = mkForce ''
mkdir -m 0755 -p /usr/bin
ln -sfn ${env-friendly}/bin/env /usr/bin/.env.tmp
mv /usr/bin/.env.tmp /usr/bin/env # atomically replace /usr/bin/env
'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment