~kaction/config

a55293961500f0f2cf646f4094e027c900681b5b — Dmitry Bogatov 2 months ago 66d30d1
Replace .posixrc aliases with small scripts
M flake.nix => flake.nix +5 -1
@@ 75,6 75,11 @@

          Documentation =
            callPackage ./universe/Documentation { inherit nixpkgs; };

          aliases = callPackage ./universe/aliases {
            inherit (pkgs.pkgsStatic) stdenv execline;
          };

          attach-shell = call ./universe/attach-shell;
          blurdate = callPackage ./universe/blurdate { };
          dbxcli = callPackage ./universe/dbxcli { };


@@ 97,7 102,6 @@
            lib.doJailbreak
            (packages.ghc884.callCabal2nix "nixfmt" nixfmt-src { });

          posixrc = callPackage ./universe/posixrc { };
          psql = pkgs.postgresql_10;
          rewrite-paths =
            { drv, rules ? [ ], buildInputs ? [ ], postBuild ? "" }:

A universe/aliases/builder.py => universe/aliases/builder.py +19 -0
@@ 0,0 1,19 @@
import json
import os
from os import environ as env

# We need something remarkable for {next} to remove, yet stable so
# existing shells don't need to update their PATH.
mark = "0" * 32
base = f"{env['out']}/bin/aliases/{mark}"
os.makedirs(base)

next_, execline_ = env["next"], env["execline"]
print(env["config"])
for name, value in json.loads(env["config"]).items():
    path = f"{base}/{name}"
    with open(path, "w") as fp:
        fp.write(f"""#!{execline_}/bin/execlineb -WS0
        {next_} {mark} {value} $@
        """)
    os.chmod(path, 0o777)

A universe/aliases/default.nix => universe/aliases/default.nix +36 -0
@@ 0,0 1,36 @@
{ stdenv, python3, execline }:
let # nixfmt: sort
  aws-env = ''
    backtick -i AWS_ACCESS_KEY_ID
      { ask https://dbogatov@aws.amazon.com/public }
    backtick -i AWS_SECRET_ACCESS_KEY
      { ask https://dbogatov@aws.amazon.com/private }
    export AWS_DEFAULT_REGION us-west-2
  '';

  # Bug: reason nixfmt does not actually sort this attrset.
  config = { # nixfmt: sort
    aws = "${aws-env} aws";
    gh = "backtick -i GITHUB_TOKEN { ask https://kaction@github.com } gh";
    ls = "ls --color=auto";
    terraform = "${aws-env} terraform";
  };

  next = stdenv.mkDerivation {
    name = "next";
    dontUnpack = true;
    installPhase = ''
      ${stdenv.targetPlatform.config}-cc ${./next.c} -o $out
    '';
  };
in stdenv.mkDerivation {
  name = "aliases";
  dontUnpack = true;
  nativeBuildInputs = [ python3 ];
  installPhase = ''
    python3 ${./builder.py}
  '';

  config = builtins.toJSON config;
  inherit next execline;
}

A universe/aliases/next.c => universe/aliases/next.c +46 -0
@@ 0,0 1,46 @@
#include <unistd.h>
#include <sysexits.h>
#include <stdlib.h>
#include <string.h>
#define write2(x) (void) write(2, x"", sizeof(x) - 1)
extern char **environ;

int
main(int argc, char **argv)
{
	char **p;
	const char *hash;

	if (argc < 3) {
		write2("usage: next {hash#32} {prog} [{arg}...]\n");
		return EX_USAGE;
	}

	hash = argv[1];
	if (strlen(hash) != 32) {
		write2("usage: next {hash#32} {prog} [{arg}...]\n");
		return EX_USAGE;
	}

	/* According to my interpretation of manual pages, modifying {environ}
	 * directly in single-threaded application is okay, but modifying
	 * string returned by getenv(3) is undefined behavior.
	 *
	 * By modifying {environ} I avoid need to do memory allocation, at
	 * least on top of what runtime already does
	 */
	for (p = environ; *p; ++p) {
		char *path = *p;
		char *begin;

		if (strncmp(path, "PATH=", 5))
			continue;
		begin = path;
		while (begin = strstr(begin, hash)) {
			begin = memset(begin, '#', 32) + 32;
		}
	}

	execvp(argv[2], argv + 2);
	return 127;
}

M universe/attach-shell/default.nix => universe/attach-shell/default.nix +0 -3
@@ 4,9 4,6 @@ let
  shellrc = writeText "shellrc" ''
    export PS1=">>= "
    unset ENV
    if [ -r ~/.nix-profile/posixrc ] ; then
      . ~/.nix-profile/posixrc
    fi
    if [ -r ~/.posixrc ] ; then
      . ~/.posixrc
    fi

M universe/default.nix => universe/default.nix +1 -1
@@ 7,6 7,7 @@ buildEnv {
  name = "universe-13";
  paths = [
    Documentation
    aliases
    acpi # Check battery status
    indent
    asciinema


@@ 16,7 17,6 @@ buildEnv {
    # usually assume GNU Make.
    gnumake
    coreutils
    posixrc
    curl
    haskellPackages.diohsc
    dvtm

M universe/neovim/boot/after/ftplugin/sh.vim => universe/neovim/boot/after/ftplugin/sh.vim +0 -5
@@ 1,7 1,2 @@
setlocal tabstop=4
setlocal shiftwidth=4

if expand("%:t") ==# '.posixrc'
  let &path = printf("%s/prefix/config/posix", expand("%:h"))
  setlocal suffixesadd+=.sh
endif

D universe/posixrc/default.nix => universe/posixrc/default.nix +0 -5
@@ 1,5 0,0 @@
{ runCommandLocal }:
runCommandLocal "posixrc" { } ''
  mkdir -p $out
  cat ${./init.sh} >> $out/posixrc
''

D universe/posixrc/init.sh => universe/posixrc/init.sh +0 -21
@@ 1,21 0,0 @@
alias ls='ls --color=auto'
alias cd-root='cd "$(git root)"'
alias gh='GITHUB_TOKEN=`ask https://kaction@github.com` command gh'

terraform() {
	AWS_ACCESS_KEY_ID=`ask https://dbogatov@aws.amazon.com/public` \
	AWS_SECRET_ACCESS_KEY=`ask https://dbogatov@aws.amazon.com/private` \
	AWS_DEFAULT_REGION=us-west-2 \
	command terraform "$@"
}

vidir() {
	if [ $# = 0 ] ; then
		find -maxdepth 1 -not -name .git -and -not -name . | command vidir -
	else
		command vidir "$@"
	fi
}

mkdir -p "/dev/shm/${USER}/volatile"
echo "${MSMTP_PASSWORD:-}" > "/dev/shm/${USER}/volatile/kaction.cc"

M universe/uenv/uenv.exec => universe/uenv/uenv.exec +1 -1
@@ 5,5 5,5 @@ if { umask 077 @busybox@/bin/mkdir -p /dev/shm/${USER}/.abduco }
redirfd -r 42 ${HOME}
redirfd -r 91 /dev/shm/${USER}
envfile @envfile@
@execline@/bin/export PATH ${HOME}/.nix-profile/bin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin
@execline@/bin/export PATH ${HOME}/.nix-profile/bin/aliases/00000000000000000000000000000000:${HOME}/.nix-profile/bin:usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin
@execline@/bin/exec $@