~kaction/config

b1783477eb7eed4eda4966c85b464173028e012a — Dmitry Bogatov 7 months ago 2000b08 + 42707d2
Merge branch 'better-cdb' into master

* better-cdb:
  nix-sys: generate build system inside of build system
  nix-sys: mark files created as immutable
  nix-sys: add rule to generate .gitignore
  nix-sys: move more of build process out of Nix
  nix-sys: do not install during configure phase
M system-v2/nix-sys-generate/nix-sys-generate.py => system-v2/nix-sys-generate/nix-sys-generate.py +3 -2
@@ 85,7 85,8 @@ def make_output_cdb(out, manifest):
@click.option("--template-directory", required=True)
@click.option("--output-config", required=True)
@click.option("--output-cdb", required=True)
def main(hash, manifest, template_directory, output_config, output_cdb):
@click.option("--staged-output-cdb")
def main(hash, manifest, template_directory, output_config, output_cdb, staged_output_cdb):
    manifest = json.loads(manifest)
    render = functools.partial(render_template, template_directory)
    manifest[CDB_PATH] = dict(path=output_cdb, action="symlink")


@@ 95,7 96,7 @@ def main(hash, manifest, template_directory, output_config, output_cdb):
            make_output_config(out, manifest, hash, render, output_cdb)

    if output_cdb:
        with open(output_cdb, "wb") as out:
        with open(staged_output_cdb or output_cdb, "wb") as out:
            make_output_cdb(out, manifest)

main()

A system-v2/nix-sys/.gitignore.do => system-v2/nix-sys/.gitignore.do +15 -0
@@ 0,0 1,15 @@
#!/bin/sh -eu

# git does not recognize ./foo as pattern matching file named "foo".
redo-targets | sed 's/..//' > $3
cat << EOF >> $3
.gitignore
.dep.*
.depend.*
.target.*
.lock.*
index.cdb
config.h
conf/*
EOF


A system-v2/nix-sys/all.do => system-v2/nix-sys/all.do +2 -0
@@ 0,0 1,2 @@
#!/bin/sh
exec redo-ifchange .gitignore nix-sys install.sh

A system-v2/nix-sys/chattr/disable.c => system-v2/nix-sys/chattr/disable.c +2 -0
@@ 0,0 1,2 @@
int immutable_on(const char *_path) { return 0; }
int immutable_off(const char *_path) { return 0; }

A system-v2/nix-sys/chattr/enable.c => system-v2/nix-sys/chattr/enable.c +35 -0
@@ 0,0 1,35 @@
#include <linux/fs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>

static int fssetflags(int fd, unsigned int flags)
{
	return ioctl(fd, FS_IOC_SETFLAGS, (void*)&flags);
}

static int path_setattrs(const char *path, unsigned int flags)
{
	int fd;
	int err;

	fd = open(path, O_NONBLOCK|O_RDONLY);
	if (fd < 0)
		return 1;
	err = fssetflags(fd, flags);
	close(fd);

	return err;
}

int immutable_on(const char *path)
{
	return path_setattrs(path, FS_IMMUTABLE_FL);
}

int immutable_off(const char *path)
{
	return path_setattrs(path, 0);
}

A system-v2/nix-sys/default.h.do => system-v2/nix-sys/default.h.do +7 -0
@@ 0,0 1,7 @@
#!/bin/sh -eu
feature=$(cat conf/feature-$2)
src="$2/$feature.c"

redo-ifchange $src conf/feature-$2

cproto -qe < $src > $3

M system-v2/nix-sys/default.nix => system-v2/nix-sys/default.nix +15 -15
@@ 1,25 1,25 @@
{ stdenv, tinycdb, nix-sys-generate, manifest ? {} }:
{ stdenv, tinycdb, nix-sys-generate, manifest ? { }, redo-c, cproto
, linuxHeaders ? null }:
assert manifest != null;
let
in stdenv.mkDerivation {
  name = "nix-sys";
  src = ./.;
  outputs = [ "out" "cdb" ];
  nativeBuildInputs = [ nix-sys-generate ];
  buildInputs = [ tinycdb ];
  nativeBuildInputs = [ nix-sys-generate redo-c cproto ];
  buildInputs = [ tinycdb linuxHeaders ];
  configurePhase = ''
    hash=$(echo ${placeholder "out"}|cut -b 12-43)
    nix-sys-generate \
      --manifest "$manifest" \
      --output-config config.h \
      --output-cdb $cdb \
      --hash $hash
  '';
  installPhase = ''
    mkdir -p $out/bin
    ${stdenv.hostPlatform.config}-cc -static -O2 -g main.c -lcdb -o $out/bin/nix-sys
    ${stdenv.hostPlatform.config}-strip -s $out/bin/nix-sys
    mkdir conf
    echo $out > conf/out
    echo $cdb > conf/cdb
    echo "$manifest" > conf/manifest
    echo "${stdenv.cc.targetPrefix}cc -static -O2" > conf/cc
    echo "${stdenv.cc.targetPrefix}cc" > conf/ld
    echo "${if linuxHeaders != null then "enable" else "disable"}" \
      > conf/feature-chattr
  '';
  buildPhase = "redo";
  installPhase = "./install.sh";
  postFixup = "rm -fr $out/nix-support";
  manifest = builtins.toJSON manifest;
}


A system-v2/nix-sys/default.o.do => system-v2/nix-sys/default.o.do +13 -0
@@ 0,0 1,13 @@
#!/bin/sh -eu
cc=$(cat conf/cc)
redo-ifchange conf/cc generate chattr.h

if [ -e "$2.c" ] ; then
	src="$2.c"
else
	redo-ifchange "conf/feature-$2"
	src="$2/$(cat conf/feature-$2).c"
fi

redo-ifchange "$src"
$cc "$src" -c -o $3

A system-v2/nix-sys/generate.do => system-v2/nix-sys/generate.do +13 -0
@@ 0,0 1,13 @@
#!/bin/sh -eu
out=$(cat conf/out)
cdb=$(cat conf/cdb)
manifest=$(cat conf/manifest)
redo-ifchange conf/out conf/cdb conf/manifest

hash=$(echo "$out" | cut -b 12-43)
exec nix-sys-generate               \
      --manifest "$manifest"        \
      --output-config config.h      \
      --output-cdb "$cdb"           \
      --staged-output-cdb index.cdb \
      --hash "$hash"

A system-v2/nix-sys/install.in => system-v2/nix-sys/install.in +6 -0
@@ 0,0 1,6 @@
#!/bin/sh -eu
redo

install -d "@out@/bin"
install -m 755 ./nix-sys "@out@/bin"
install -m 444 ./index.cdb "@cdb@"

A system-v2/nix-sys/install.sh.do => system-v2/nix-sys/install.sh.do +7 -0
@@ 0,0 1,7 @@
#!/bin/sh
redo-ifchange conf/out conf/cdb install.in
out=$(cat conf/out)
cdb=$(cat conf/cdb)

sed -e "s#@out@#${out}#g" -e "s#@cdb@#${cdb}#g" < install.in > "$3"
chmod +x "$3"

M system-v2/nix-sys/main.c => system-v2/nix-sys/main.c +11 -0
@@ 8,6 8,8 @@
#include <unistd.h>
#include <stdio.h>

#include "chattr.h"

struct copy_a {
	const char *source;
	const char *tmp;


@@ 263,7 265,16 @@ int main(int argc, char **argv)
		write2("renaming copy into ");
		write2_s(obj->destination);
		write2("... ");

		/* Make file immutable (chattr +i) so it can't be
		 * modified by mistake. Linux-only.
		 *
		 * TODO: Check errors properly.
		 */
		(void) immutable_off(obj->destination);
		err = rename(obj->tmp, obj->destination);
		(void) immutable_on(obj->destination);

		if (err) {
			write2("fail\n");
			return 1;

A system-v2/nix-sys/nix-sys.do => system-v2/nix-sys/nix-sys.do +5 -0
@@ 0,0 1,5 @@
#!/bin/sh -eu
ld=$(cat conf/ld)
objs="main.o chattr.o"
redo-ifchange conf/ld $objs
$ld $objs -lcdb -o $3