Skip to content

Instantly share code, notes, and snippets.

View tazjin's full-sized avatar
🥝
computers

tazjin

🥝
computers
View GitHub Profile
@tazjin
tazjin / journald_log4rs.rs
Created September 24, 2018 17:07
journald priority decorator for log4rs
/// Implementation of a log-encoder decorator that can prepend
/// journald log priorities to any given `log4rs`-encoder.
#[derive(Debug)]
struct JournaldDecorator<E: Encode>(E);
/// Helper function to map from `log`-crate log levels to journald log
/// priorites. Please see `sd-daemon(3)` for detailed information on
/// these levels.
fn journald_priority(level: &Level) -> &'static str {
match level {
@tazjin
tazjin / Door.hs
Last active August 31, 2018 12:35
FSM typeclass demonstration
{-# LANGUAGE MultiWayIf #-}
-- | This module implements a door that can be opened and closed, and
-- locked/unlocked with a specified code when in closed state.
--
-- The state diagram looks roughly like this (p. simple):
--
-- <--Open--- <--Unlock--
-- [Opened] [Closed] [Locked]
-- --Close--> ----Lock-->
@tazjin
tazjin / Migratatable.hs
Last active August 9, 2018 09:31
Type-level Haskell programming to ensure that type versions are "migratable"
-- | A type-level, constraint-writing function that ensures that some
-- versioned type is migratable throughout its entire version history.
--
-- Assumes that type version history starts at '1'.
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
-- | Test something with an in-memory acid-state
acidTest :: forall state event query result.
(IsAcidic state, UpdateEvent event, MethodState event ~ state,
QueryEvent query, MethodState query ~ state,
MethodResult query ~ result)
=> String -- ^ Test description
-> state -- ^ Initial state for test
-> event -- ^ Event to test
-> query -- ^ Query to check
-> (result -> Bool) -- ^ Function to test query result
@tazjin
tazjin / magit-push-advise.el
Last active July 13, 2018 11:25
Advise magit-git-push to ask for user confirmation
;; This demonstrates how to advise[1] magit functions that perform
;; potentially destructive operations (for example `magit-git-push')
;; with a confirmation prompt.
;;
;; The TL;DR of advising is that a function is "wrapped" around the
;; function to be advised, with various different behaviours that can
;; be used (e.g. only conditionally executing the original function).
;;
;; [1]: https://www.gnu.org/software/emacs/manual/html_node/elisp/Advising-Functions.html#Advising-Functions
;;
#!/usr/bin/env bash
set -e
function cleanup {
git checkout src/ibrowse.erl
}
trap cleanup EXIT
echo "Patching in main/1 function with hex.pm test"
# Generate an immutable /etc/resolv.conf from the networking.nameservers setting:
environment.etc."resolv.conf" = with lib; with pkgs; {
source = writeText "resolv.conf" ''
${concatStringsSep "\n" (map (ns: "nameserver ${ns}") config.networking.nameservers)}
options edns0
'';
};
@tazjin
tazjin / crius-struct.rs
Created May 10, 2018 14:51
Storing Rust crius circuit breakers in structs
extern crate crius;
use crius::command::{RunnableCommand, Command, CommandError};
/// Convenience type alias for breaker function pointers.
pub type BreakerFn<I, O> = fn(I) -> Result<O, Box<CommandError>>;
/// Convenience type alias for working with crius commands:
pub type Breaker<I, O> =
RunnableCommand<I, O,
BreakerFn<I,O>,
with import <nixpkgs> {};
stdenv.mkDerivation rec {
name = "stern-${version}";
version = "1.5.1";
src = fetchurl {
url = "https://github.com/wercker/stern/releases/download/${version}/stern_linux_amd64";
sha256 = "0ki2l22q1djqcqaxrv25fwljkjyzhvp5slc1yxchnyadilaifk3y";
name = "stern";
@tazjin
tazjin / nix-notes.org
Last active October 24, 2017 15:52
Notes on Nix(OS)

Notes on Nix(OS)

This file contains various notes about issues I encountered when using NixOS and the Nix package manager.

Note: I think that Nix is fundamentally a good concept and it is also, in practice, one of the better solutions for both package and configuration management.

Note also: This file is very, very WIP.

There is no documentation about how things work