Skip to content

Instantly share code, notes, and snippets.

View jis_enc.rb
#!/usr/bin/env ruby
bytes=(0..255).to_a
valid=bytes.product(bytes)
.map{|a,b|(a.chr+b.chr).force_encoding('EUC-JISX0213')}
.select &:valid_encoding?
jis_only = valid.select{ |s| s.encode('UTF-8', undef: :replace) == "\uFFFD" }
# this will obtain only the valid characters that we can't convert to unicode
@berdario
berdario / _utils.py
Last active Aug 29, 2015
A simple python hook system that doesn't mutate global variables and tries to be as side-effect free as possible
View _utils.py
from __future__ import print_function
import sys
from collections import namedtuple
from imp import load_module, find_module
from pathlib import Path
Hooks = namedtuple('Hooks', 'preactivate postactivate get_env_details postrmvirtualenv')
_HOOKS = Hooks._fields
@berdario
berdario / withFiles.hs
Created Jan 16, 2015
I felt the need for an n-ary version of withFile. It works, but for something this simple, readFile + writeFile would've been enough
View withFiles.hs
import System.Environment (getArgs)
import System.IO (withFile, IOMode(..), FilePath, Handle, hGetContents, hPutStr)
import Data.Functor ((<$>))
withFiles' :: [Handle] -> [(IOMode, FilePath)] -> ([Handle] -> IO r) -> IO r
withFiles' handles [] f = f $ reverse handles
withFiles' handles ((mode, fpath):xs) f = withFile fpath mode (\x -> withFiles' (x:handles) xs f)
withFiles = withFiles' []
View nixnotes.md

It's been a while since I started to use Nixos, and I promised myself to improve the documentation/wiki, but since I haven't overcome yet the barrier of entry to that, and some of the information that I discovered/asked on irc/etc might otherwise be forgotten, I'll temporarily keep track of it here:

  • Don't rely too much on nix-shell/nix-build: the Phases will kick you out of the shell if an error happens, and you won't be able to load the correct scripts, I reported this here but I'll probably need to add some more details
  • Apparently there's no way to see software that is available for another platform (this would be useful in a porting effort): you have to manually grep nixpkgs yourself
  • There's no way to do a partial match query on nix-env: just nix-env -qaP "*" and grep it
  • The nix manual is seriously paltry when discussing the Nix language itself: some operators aren't even mentioned (e.g. <)
  • If
@berdario
berdario / fulltextindex.hs
Created Feb 11, 2015
A simple text index/full text search in Haskell (I wanted a simple, but non-trivial fold example)
View fulltextindex.hs
import qualified Data.Map.Strict as M
import qualified Data.Set as S
docs = ["the quick brown fox jumps over the lazy dog",
"what does the fox say?",
"what is the meaning of life?"]
buildIndex docs = M.unionsWith S.union [foldr (flip M.insert $ S.singleton doc) M.empty $ words doc | doc <- docs]
search _ [] = S.empty
View wordwrap.scala
package object wordwrap{
def wrapSingleLine(column: Int)(words: List[String]): Iterable[String] = words match {
case List() => List();
case (hd :: tl) => {
tl.scanLeft(hd)({_ + " " + _}).span({_.length <= column}) match {
case (List(), _) => hd.grouped(column).toIterable ++ wrapSingleLine(column)(tl)
case (f, s) => {
val f2 = f.map({_.split(" ").last})
f2 ++ wrapSingleLine(column)(s.map{_.drop(f2.last.length)});
}
View gist:a33570d1f593c06ea328
# I use this both inside configuration.nix with
## environment.systemPackages = (import ((builtins.getEnv "PWD") + "/packages.nix" ));
# and on the command line with
## nix-env -I ~ -f packages.nix -iA
#with (import (builtins.getEnv "HOME" + "/.nix-defexpr/channels_root/nixos/default.nix") {}).pkgs;
with (import "/home/dario/.nix-defexpr/channels_root/nixos/default.nix" {}).pkgs;
[
# software available in the nixos nix-channel
] ++
View -
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:386)
at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
Caused by: java.lang.UnsatisfiedLinkError: /usr/lib/jayatana/libjayatanaag.so: libX11.so.6: cannot open shared object file: No such file or directory
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
View -
execve("/home/dario/.nix-profile/bin/java", ["java", "-jar", "/home/dario/Downloads/burpsuite_"...], [/* 74 vars */]) = 0
brk(0) = 0x1c09000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f53fc6f9000
readlink("/proc/self/exe", "/nix/store/nmd034pr2aa1brz015gix"..., 4096) = 80
access("/etc/ld-nix.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/nix/store/nmd034pr2aa1brz015gixp0zni9r2w7f-openjdk-8u40b25/lib/openjdk/bin/../lib/amd64/jli/tls/x86_64/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/nix/store/nmd034pr2aa1brz015gixp0zni9r2w7f-openjdk-8u40b25/lib/openjdk/bin/../lib/amd64/jli/tls/x86_64", 0x7ffece51e4f0) = -1 ENOENT (No such file or directory)
open("/nix/store/nmd034pr2aa1brz015gixp0zni9r2w7f-openjdk-8u40b25/lib/openjdk/bin/../lib/amd64/jli/tls/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/nix/store/nmd034pr2aa1brz015gixp0zni9r2w7f-openjdk-8u40b25/
View -
This file has been truncated, but you can view the full file.
execve("/home/dario/.nix-profile/bin/java", ["java", "-jar", "/home/dario/Downloads/burpsuite_"...], [/* 75 vars */]) = 0
brk(0) = 0x10b8000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f19e5550000
readlink("/proc/self/exe", "/nix/store/nmd034pr2aa1brz015gix"..., 4096) = 80
access("/etc/ld-nix.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/nix/store/nmd034pr2aa1brz015gixp0zni9r2w7f-openjdk-8u40b25/lib/openjdk/bin/../lib/amd64/jli/tls/x86_64/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/nix/store/nmd034pr2aa1brz015gixp0zni9r2w7f-openjdk-8u40b25/lib/openjdk/bin/../lib/amd64/jli/tls/x86_64", 0x7ffe5c7c7390) = -1 ENOENT (No such file or directory)
open("/nix/store/nmd034pr2aa1brz015gixp0zni9r2w7f-openjdk-8u40b25/lib/openjdk/bin/../lib/amd64/jli/tls/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)